Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Admin ignores null=True in ForeignKey [RSS Feed]

#1 Jan. 10, 2011 20:28:06

David W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Admin ignores null=True in ForeignKey


If I create a ForeignKey relationship between two Models with null=True, the
Admin seems to still insist on there being a foreign object to link to.

For example, I created a very simple ForeignKey relationship from Things to
Types:

from django.db import models

class Type(models.Model):
name = models.CharField(max_length=30)

class Thing(models.Model):
type_of_thing = models.ForeignKey(Type, null=True)
name = models.CharField(max_length=30)

In the console, I can happily create Type-less Things:

>>> from ni.models import *
>>> x = Thing()
>>> x.name = 'Untyped thing'
>>> x.type = None
>>> x.save()
>>> all_things = Thing.objects.all()
>>> print all_things

>>> print all_things.type_of_thing
None

In the Admin, however, when I try to add a new Thing, or even edit and save
the existing Thing, the type_of_thing field is shown as required and saving
without setting it causes "This field is required." to be displayed and the
Thing not to be saved.

Am I doing something wrong?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 10, 2011 22:49:07

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Admin ignores null=True in ForeignKey


On Monday, January 10, 2011 8:22:01 PM UTC, David Walker wrote:
>
> If I create a ForeignKey relationship between two Models with null=True,
> the Admin seems to still insist on there being a foreign object to link to.
>
> For example, I created a very simple ForeignKey relationship from Things to
> Types:
>
> from django.db import models
>
> class Type(models.Model):
> name = models.CharField(max_length=30)
>
> class Thing(models.Model):
> type_of_thing = models.ForeignKey(Type, null=True)
> name = models.CharField(max_length=30)
>
> In the console, I can happily create Type-less Things:
>
> >>> from ni.models import *
> >>> x = Thing()
> >>> x.name = 'Untyped thing'
> >>> x.type = None
> >>> x.save()
> >>> all_things = Thing.objects.all()
> >>> print all_things
>
> >>> print all_things.type_of_thing
> None
>
> In the Admin, however, when I try to add a new Thing, or even edit and save
> the existing Thing, the type_of_thing field is shown as required and saving
> without setting it causes "This field is required." to be displayed and
> the Thing not to be saved.
>
> Am I doing something wrong?
>


Yes - you're not reading the documentation correctly.
>Fromhttp://docs.djangoproject.com/en/1.2/topics/db/models/#field-options:

Note that this is different than
null<http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.Field.null>
.
null<http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.Field.null>
is
purely database-related, whereas
blank<http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.Field.blank>
is
validation-related. If a field has
blank=True<http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.Field.blank>,

validation on Django's admin site will allow entry of an empty value. If a
field has
blank=False<http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.Field.blank>,

the field will be required.


So to allow empty entries in the admin, you need to use "blank=True,
null=True".
--
DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Admin ignores null=True in ForeignKey [RSS Feed]

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support