views:

124

answers:

2

I've a model like this with django 1.1:

class Booking(models.Model):
  name = models.CharField(max_length=100)

By default, I'm reading that both 'null' and 'blank' are False.

So with a test like this...

class SimpleTest(TestCase):
    def test_booking_save(self):
        b = Booking()
        b.save()

... I expected the save to throw an exception. But it doesn't. It seems quite happy to create a new record with a blank name (postgres and sqlite3).

I note that via the admin interface a save does indeed fail with a "this field is required".

Questions are:

  1. Is the 'blank' attribute only applied by forms?
  2. Is the fix to override the save() method and explicitly check that len(name) != 0?
  3. Have I misunderstood something which once understood resolves my misunderstanding?
A: 

From the Django Docs:

"Note that empty string values will always get stored as empty strings, not as NULL. Only use null=True for non-string fields such as integers, booleans and dates."

Your code is storing an empty string.

To illustrate this, try:

class SimpleTest(TestCase):
    def test_booking_save(self):
        b = Booking()
        b.name = None
        b.save()
Harold
True, but doesn't answer the question asked.
Carl Meyer
Yes it is indeed storing an empty string. That's the problem. As 'blank' is set to 'False' i'd expected it to prevent that.
John Mee
+3  A: 

blank=True/False only applies to forms. Data validation currently only happens at the form level; this will change when the model-validation Google Summer of Code work gets merged in to trunk.

The only kind of validation that currently happens at the model layer is whatever errors your database backend will throw if it can't handle what it gets. In the case of an empty CharField you'll generally never get errors from the database, as Django sets the field to an empty string by default.

For now, you should use the save() method for any model-level validation you want (you can even use this trick to automatically get all the form-level validation at the model level). Soon (if you're on trunk) or when 1.2 comes out, use the model validation stuff.

Carl Meyer