Charjield django model: max_length not working?

I am trying to create a limited selection field:

Action_Types=( ('0','foo'), ('1','bar'), ) class Foo(models.Model): myAction=models.CharField(max_length=1,choices=Action_Types) def __unicode__(self): return '%d %s'%(self.pk,self.myAction) 

However, when I tried to insert content that violated the rules, it succeeded without any error messages or warnings (using the "manage.py" shell). It seems that any text of any length can be placed in this field. I am using SQLite3 as a backend.

Is this supposed to be so? Or if I missed something?

+8
django sqlite maxlength model
source share
1 answer

SQLite does not provide VARCHAR duration.

From SQLite Frequently Asked Questions :

(9) What is the maximum size of a VARCHAR in SQLite?

SQLite does not provide VARCHAR duration. You can declare VARCHAR (10) and SQLite will be happy to provide you 500 characters. And it will keep all 500 characters intact - it never truncates.

If you upgrade the database using the django administration forms or model, Django will do a length check for you. In the shell, you can manually call full_clean before saving and catch a validation error.

 f = Foo(myAction="more than 1 char") try: f.full_clean() f.save() except ValidationError as e: # Do something based on the errors contained in e.message_dict. 
+16
source share

All Articles