Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CharField with fixed length, how?

Tags:

django

I wold like to have in my model a CharField with fixed length. In other words I want that only a specified length is valid.

I tried to do something like

volumenumber = models.CharField('Volume Number', max_length=4, min_length=4) 

but it gives me an error (it seems that I can use both max_length and min_length at the same time).

Is there another quick way?

My model is this:

class Volume(models.Model):     vid = models.AutoField(primary_key=True)     jid = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name = "Journal")     volumenumber = models.CharField('Volume Number')     date_publication = models.CharField('Date of Publication', max_length=6, blank=True)     class Meta:         db_table = u'volume'         verbose_name = "Volume"         ordering = ['jid', 'volumenumber']         unique_together = ('jid', 'volumenumber')     def __unicode__(self):         return (str(self.jid) + ' - ' + str(self.volumenumber)) 

What I want is that the volumenumber must be exactly 4 characters.

I.E. if someone insert '4b' django gives an error because it expects a string of 4 characters.

So I tried with

volumenumber = models.CharField('Volume Number', max_length=4, min_length=4) 

but it gives me this error:

Validating models... Unhandled exception in thread started by <function inner_run at 0x70feb0> Traceback (most recent call last):   File "/Library/Python/2.5/site-packages/django/core/management/commands/runserver.py", line 48, in inner_run     self.validate(display_num_errors=True)   File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 249, in validate     num_errors = get_validation_errors(s, app)   File "/Library/Python/2.5/site-packages/django/core/management/validation.py", line 28, in get_validation_errors     for (app_name, error) in get_app_errors().items():   File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 131, in get_app_errors     self._populate()   File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 58, in _populate     self.load_app(app_name, True)   File "/Library/Python/2.5/site-packages/django/db/models/loading.py", line 74, in load_app     models = import_module('.models', app_name)   File "/Library/Python/2.5/site-packages/django/utils/importlib.py", line 35, in import_module     __import__(name)   File "/Users/Giovanni/src/djangoTestSite/../djangoTestSite/journaldb/models.py", line 120, in <module>     class Volume(models.Model):   File "/Users/Giovanni/src/djangoTestSite/../djangoTestSite/journaldb/models.py", line 123, in Volume     volumenumber = models.CharField('Volume Number', max_length=4, min_length=4) TypeError: __init__() got an unexpected keyword argument 'min_length' 

That obviously doesn't appear if I use only "max_length" OR "min_length".

I read the documentation on the django web site and it seems that I'm right (I cannot use both together) so I'm asking if there is another way to solve the problem.

like image 478
Giovanni Di Milia Avatar asked Mar 18 '10 14:03

Giovanni Di Milia


People also ask

What is the max length of CharField?

CharField since CharField has a max length of 255 characters while TextField can hold more than 255 characters.

How do you use CharField?

How to use CharField ? CharField is used for storing small sized strings in the database. One can store First Name, Last Name, Address Details, etc. CharField should be given an argument max_length for specifying the maximum length of string it is required to store.


2 Answers

Kind of along the same lines as above, but for what it's worth you could also go ahead with MinLengthValidator which django supplies. Worked for me. The code would look something like this:

from django.core.validators import MinLengthValidator ... class Volume(models.Model): volumenumber = models.CharField('Volume Number', max_length=4, validators=[MinLengthValidator(4)]) ... 
like image 55
Chetan Avatar answered Sep 19 '22 17:09

Chetan


You don't even have to write a custom one. Just use the RegexValidator which Django supplies.

from django.core.validators import RegexValidator  class MyModel(models.Model):     myfield = models.CharField(validators=[RegexValidator(regex='^.{4}$', message='Length has to be 4', code='nomatch')]) 

From the Django Docs: class RegexValidator(\[regex=None, message=None, code=None\])

regex: A valid regular expression to match. For more on regex in Python check this excellent HowTo: http://docs.python.org/howto/regex.html

message: The message returned to the user in case of failure.

code: error code returned by ValidationError. Not important for your usage case, you can leave it out.

Watch out, the regex suggested by me will allow any characters including whitespace. To allow only alphanumeric characters, substitute the '.' with '\w' in the regex argument. For other requirements, ReadTheDocs ;).

like image 22
tBuLi Avatar answered Sep 18 '22 17:09

tBuLi