null=False, blank=True and errors saving in admin?

98 views
Skip to first unread message

ringemup

unread,
Jun 24, 2010, 5:40:50 PM6/24/10
to Django users
Upgrading from Django 1.0 to 1.2, I'm suddenly getting errors when
creating objects in the admin form a class that essentially looks like
this:

class MyObject(models.Model):
name = models.CharField(max_length=50)
other_thing = models.ForeignKey(MyOtherModel, null=False,
blank=True)

def save(self, *args, **kwargs):
logging.debug('saving here')
if not self.other_thing:
other_thing = MyOtherModel()
other_thing.save()
self.other_thing = other_thing
super(MyObject, self).save(*args, **kwargs)

Creating a MyObject in the admin and leaving the other_thing field
blank results in the following error:

Cannot assign None: "MyObject.other_thing" does not allow null values.

The logging statement is never being hit, so it looks like the over-
aggressive validation is being performed in the ModelForm. What am I
doing wrong?

Thanks!

Karen Tracey

unread,
Jun 24, 2010, 6:22:19 PM6/24/10
to django...@googlegroups.com
On Thu, Jun 24, 2010 at 5:40 PM, ringemup <ring...@gmail.com> wrote:
Upgrading from Django 1.0 to 1.2, I'm suddenly getting errors when
creating objects in the admin form a class that essentially looks like
this:

class MyObject(models.Model):
 name = models.CharField(max_length=50)
 other_thing = models.ForeignKey(MyOtherModel, null=False,
blank=True)

 def save(self, *args, **kwargs):
   logging.debug('saving here')
   if not self.other_thing:
     other_thing = MyOtherModel()
     other_thing.save()
     self.other_thing = other_thing
  super(MyObject, self).save(*args, **kwargs)

Creating a MyObject in the admin and leaving the other_thing field
blank results in the following error:

Cannot assign None: "MyObject.other_thing" does not allow null values.

There is a ticket open on this: http://code.djangoproject.com/ticket/13776

Karen
--
http://tracey.org/kmt/

ringemup

unread,
Jun 24, 2010, 6:45:05 PM6/24/10
to Django users

Thanks, that didn't show up in my search, but it's exactly the same
traceback.

The ticket description seems to indicate the bug only applies to
testing situations, but clearly it occurs in the default ModelForm
(and thereby in the admin) any time a foreign key is set to
null=False, blank=True. Leaving a field blank and filling it in in
the model's save() method seems to be a pretty common case, and I
would think should be allowed without creating a custom ModelForm.

Should this be listed somewhere as a backwards-incompatible change,
since it worked in 1.0?



On Jun 24, 6:22 pm, Karen Tracey <kmtra...@gmail.com> wrote:

Karen Tracey

unread,
Jun 24, 2010, 7:01:01 PM6/24/10
to django...@googlegroups.com
On Thu, Jun 24, 2010 at 6:45 PM, ringemup <ring...@gmail.com> wrote:

Thanks, that didn't show up in my search, but it's exactly the same
traceback.

The ticket description seems to indicate the bug only applies to
testing situations, but clearly it occurs in the default ModelForm
(and thereby in the admin) any time a foreign key is set to
null=False, blank=True.  Leaving a field blank and filling it in in
the model's save() method seems to be a pretty common case, and I
would think should be allowed without creating a custom ModelForm.

Should this be listed somewhere as a backwards-incompatible change,
since it worked in 1.0?


I don't believe it was an intentional change in behavior, thus I think it is a bug that should be fixed, not a change that should be documented. But I'm not all that familiar with the model validation changes, so I'm not 100% sure of that nor do I know the right way to fix it.

Karen
Reply all
Reply to author
Forward
0 new messages