0

I have this simple Blog model:

class Blog(models.Model):

    title = models.CharField(_('title'), max_length=60, blank=True, null=True)
    body = models.TextField(_('body'))
    user = models.ForeignKey(User)  
    is_public = models.BooleanField(_('is public'), default = True)

When I insert a blog in admin interface, I get this error:

IntegrityError at /admin/blogs/blog/add/

null value in column "is_public" violates not-null constraint

Why ???

xRobot
  • 25,579
  • 69
  • 184
  • 304

1 Answers1

0

Hmm. Odd quirk. Can't say why it is happening because the default should mean it's always set, but this should fix it, if you apply the relevant schema change/migration

is_public = models.BooleanField(_('is public'), default=True, null=True)

Steve Jalim
  • 11,989
  • 1
  • 37
  • 54
  • 1
    I belive you can't have null=True on BooleanField, you need to use [NullBooleanField](http://docs.djangoproject.com/en/dev/ref/models/fields/#nullbooleanfield) – Davor Lucic May 27 '10 at 17:40
  • If I add null = True, like you said, I get this error: Error: One or more models did not validate: blogs.blog: "is_public": BooleanFeilds do not accept null values. Use a NullBooleanField instead. So now I am using NullBooleanFields and it does work. But Why I have to do this ? It's strange :-\ – xRobot May 27 '10 at 17:40