Django model CharField: max_length does not work?

SQLite does not enforce the length of a VARCHAR.

Up vote 0 down vote favorite share g+ share fb share tw.

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?

Or if I missed something? Django sqlite model choice maxlength link|improve this question asked Dec 12 '11 at 17:10Xun Yang996 61% accept rate.

SQLite does not enforce the length of a VARCHAR. From the SQLite Frequently asked questions: (9) What is the maximum size of a VARCHAR in SQLite? SQLite does not enforce the length of a VARCHAR.

You can declare a VARCHAR(10) and SQLite will be happy to let you put 500 characters in it. And it will keep all 500 characters intact - it never truncates. 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_clean before saving, and catch the validation error. F = Foo(myAction="more than 1 char") try: f. Full_clean() f.save() except ValidationError, e: # Do something based on the errors contained in e.

Message_dict.

Thanks for the answer, Alasdair! Seems like the only place to check correctness is by using a full_clean(). – Xun Yang Dec 12 '11 at 17:21.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions