[Answered ]-What's the best way to define a particular number with special characters in a database django

1๐Ÿ‘

โœ…

You can define a validator and work with a CharField and a RegexValidator [Django-doc]:

from django.db import models
from django.core.validators import RegexValidator

class MyModel(models.Model):
    my_field = models.CharField(
        max_length=9,
        validators=[RegexValidator(r'\d{4}.\d{4}', 'The value should be four digits, a character and four digits')]
    )

If the separator between the four digits is always a hyphen, you use r'\d{4}-\d{4}' instead.

Leave a comment