#33111: Conditionally changing ModelAdmin inlines based on object's field breaks
when changing object and new inlines should appear.
-------------------------------+------------------------------------
     Reporter:  joeli          |                    Owner:  nobody
         Type:  Bug            |                   Status:  new
    Component:  contrib.admin  |                  Version:  3.2
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Accepted
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

 * cc: Hasan Ramezani, WeizhongTu (added)
 * stage:  Unreviewed => Accepted


Comment:

 Thanks for the report. I was able to fix this issue by passing an old
 instance to the `get_inlines()` 🤔:
 {{{
 diff --git a/django/contrib/admin/options.py
 b/django/contrib/admin/options.py
 index 1e5fab917e..7b2b893c10 100644
 --- a/django/contrib/admin/options.py
 +++ b/django/contrib/admin/options.py
 @@ -1585,12 +1585,16 @@ class ModelAdmin(BaseModelAdmin):
          )
          if request.method == 'POST':
              form = ModelForm(request.POST, request.FILES, instance=obj)
 +            formsets, inline_instances = self._create_formsets(
 +                request,
 +                form.instance if add else obj,
 +                change=not add,
 +            )
              form_validated = form.is_valid()
              if form_validated:
                  new_object = self.save_form(request, form, change=not
 add)
              else:
                  new_object = form.instance
 -            formsets, inline_instances = self._create_formsets(request,
 new_object, change=not add)
              if all_valid(formsets) and form_validated:
                  self.save_model(request, new_object, form, not add)
                  self.save_related(request, form, formsets, not add)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33111#comment:1>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.d802d1f4e07d83b2e4961a1eb6797352%40djangoproject.com.

Reply via email to