stephenmcd / grappelli-safe

Admin skin for Mezzanine
Other
25 stars 59 forks source link

Fixes an issue with inline formset pk hidden field being visible when it shouldn't be. #85

Closed Rundll closed 7 years ago

Rundll commented 7 years ago

Hide the inline auto pk field when the model primary key field is editable/in the form.

stephenmcd commented 7 years ago

I'm not really across this - could you go into more detail and/or provide an example?

Rundll commented 7 years ago

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.

stephenmcd commented 7 years ago

Thanks a lot