I’m trying to make a field with limited choices:
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 was trying to insert content violating the rules, it succeeded without any error or warning messages (with "manage.py shell"). It seems any text of any length can be put into this field. I’m using SQLite3 as the backend.
Is it supposed to be like that?
SQLite does not enforce the length of a VARCHAR.
From the SQLite Frequently asked questions:
If you update the database using the django admin or model forms, Django will do the length validation for you. In the shell, you could manually call
full_cleanbefore saving, and catch the validation error.