Hi django-dev,

I have often come across bugs in code where attributes of a model
instance were being assigned invalid values like None. Later on
someone tries to save the model which blows up with an IntegrityError.

Say you have a model like this:

class User(models.Model):
    name = models.CharField(max_length=20)

I can assign anything I want to this attribute:

bill = User(name='bill')
bill.name = None
bill.name = 1
bill.name = {}
bill.name = User

At some point I'll try to do bill.save() and the database will reject
my incorrectly typed values. But in a large codebase this might happen
far away from the point where the bad assignment was made and it's
hard to track it down.

Now in the case of sqlite, which is very permissive, it will happily
accept values like 1 and {} and store them as "1" and "{}" (wtf)
respectively. So if I'm running my tests against sqlite I won't even
know that this will bite me in production.

The thing is that it would be pretty easy to provide a strongly typed
model layer using descriptors. So that every time I assign to
bill.name it will raise ValidationError if the value isn't a string,
if the string is longer than max_length etc. Is there a rationale for
why we don't do this?


Martin

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAH88djXSeiKE71R46pvQDBtrnJ%3Ddz2JA6ow1dAGL%3Dwsz4dxS_w%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to