Why is django admin not accepting Nullable foreign keys?
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 nu开发者_高级运维llable. 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?
How about blank=True
? From Django's docs:
If
True
, the field is allowed to be blank. Default isFalse
.
You should try adding null=True, like this one:
car = models.ForeignKey(Car, default=None, null=True, blank=True)
...ok, I found out how to do this on my own. It might not be the best way, but I created a custom Form to validate the model:
class AdminImportRuleForm(forms.ModelForm):
class Meta:
model = ImportRule
name = forms.CharField(max_length=255)
feed = forms.ModelChoiceField(required=True, queryset=Feed.objects.all())
feed_provider_category = forms.ModelChoiceField(required=False, queryset=FeedProviderCategory.objects.all())
target_subcategories = forms.ModelMultipleChoiceField(queryset=Subcategory.objects.all())
And I linked it into the Admin class:
class ImportRuleAdmin(admin.ModelAdmin):
form = AdminImportRuleForm
...
This took a lot of working out and a lot of unnecessary bother, so if anyone else has a better solution they get vote-ups/accepts/whatever it will let me give :)
精彩评论