#33111: Conditionally changing ModelAdmin inlines based on object's field breaks saving object in admin site when new inlines should appear -----------------------------------------+------------------------ Reporter: joeli | Owner: nobody Type: Bug | Status: new Component: contrib.admin | Version: 3.2 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -----------------------------------------+------------------------ Minimal example:
{{{ #!python # models.py class Parent(models.Model): show_inlines = models.BooleanField(default=False) class Child(models.Model): parent = models.ForeignKey(Parent, on_delete=models.CASCADE) # admin.py class ChildInline(admin.StackedInline): model = Child @admin.register(Parent) class ParentAdmin(admin.ModelAdmin): def get_inlines(self, request, obj): if obj is not None and obj.show_inlines: return [ChildInline] return [] }}} - Create `Parent` objects in either initial state and it works as you'd expect, where `ChildInline` is rendered when `show_inlines` is True. - When `show_inlines` is True, you can also set it to False from the admin site, and the `ChildInline` disappears as expected. - But when `show_inlines` is False, you cannot re-enable it. Saving the object fails due to a validation error in the new `ChildInline` that didn't exist before saving: {{{ (Hidden field TOTAL_FORMS) This field is required. (Hidden field INITIAL_FORMS) This field is required. ManagementForm data is missing or has been tampered with. Missing fields: child_set-TOTAL_FORMS, child_set-INITIAL_FORMS. You may need to file a bug report if the issue persists. }}} -- Ticket URL: <https://code.djangoproject.com/ticket/33111> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/049.6109b94301e84a649527fc9c43222e5d%40djangoproject.com.