Why is django admin not accepting Nullable foreign keys?
Posted
by p.g.l.hall
on Stack Overflow
See other posts from Stack Overflow
or by p.g.l.hall
Published on 2010-03-22T15:14:20Z
Indexed on
2010/03/22
16:01 UTC
Read the original article
Hit count: 306
Here is a simplified version of one of my models:
class ImportRule(models.Model):
feed = models.ForeignKey(Feed)
name = models.CharField(max_length=255)
feed_provider_category = models.ForeignKey(FeedProviderCategory, null=True)
target_subcategories = models.ManyToManyField(Subcategory)
This class manages a rule for importing a list of items from a feed into the database.
The admin system won't let me add an ImportRule without selecting a feed_provider_category despite it being declared in the model as nullable. The database (SQLite at the moment) even checks out ok:
>>> .schema
...
CREATE TABLE "someapp_importrule" (
"id" integer NOT NULL PRIMARY KEY,
"feed_id" integer NOT NULL REFERENCES "someapp_feed" ("id"),
"name" varchar(255) NOT NULL,
"feed_provider_category_id" integer REFERENCES "someapp_feedprovidercategory" ("id"),
);
...
I can create the object in the python shell easily enough:
f = Feed.objects.get(pk=1)
i = ImportRule(name='test', feed=f)
i.save()
...but the admin system won't let me edit it, of course. How can I get the admin to let me edit/create objects without specifying that foreign key?
© Stack Overflow or respective owner