Closed Rundll closed 7 years ago
I'm not really across this - could you go into more detail and/or provide an example?
This is an edge case where you have a Model with a field specifying primary_key=True
which is used in Django admin inline with a foreign key to a parent model. When a parent ModelAdmin specifies the first model inline as StackedInline or TabularInline, the pk field on the first Model which should be hidden becomes visble at the end of the form, since it is now editable, and is incorrectly included in the form twice.
# Models
class ParentModel(models.Model):
void = models.CharField("Hiya", max_length=20)
class InlineModel(models.Model):
i_want_to_edit_my_pk = models.IntegerField("Editable pk field", blank=False, primary_key=True)
parent = models.ForeignKey(ParentModel)
# ModelAdmin
class InlineModelStackedInline(admin.StackedInline):
model = InlineModel
class ParentModelAdmin(admin.ModelAdmin):
model = ParentModel
inlines = [InlineModelStackedInline]
# the inline form on ParentModelAdmin will now show two pk fields for InlineModel in the Django admin, one in the normal fields collected for the form, and one which is usually hidden (but becomes visible).
Hope that makes sense, I copied the fix from the Django admin templates in contrib that match these templates.
Thanks a lot
Hide the inline auto pk field when the model primary key field is editable/in the form.