Opened 3 years ago

Last modified 3 years ago

#33527 closed Cleanup/optimization

remove unnecessary code from patch before in ModelAdmin inlines save. — at Version 1

Reported by: Maxim Danilov Owned by: nobody
Component: contrib.admin Version: 4.0
Severity: Normal Keywords: modeladmin, inlinemodel
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Tim Graham)

In #33111, the following code was added:

form = ModelForm(request.POST, request.FILES, instance=obj)
formsets, inline_instances = self._create_formsets(request, form.instance if add else obj, change=not add)

it is all ok, but if we have:

form = ModelForm(request.POST, request.FILES, instance=obj)

this is superabundant:

form.instance if add else obj

I think, it should be:

form = ModelForm(request.POST, request.FILES, instance=obj)
formsets, inline_instances = self._create_formsets(request, form.instance, change=not add)
# or # formsets, inline_instances = self._create_formsets(request, obj, change=not add)

The same changes is possible to made in code-block for request.GET

Change History (1)

comment:1 by Tim Graham, 3 years ago

Cc: Mariusz Felisiak removed
Description: modified (diff)

I don't see any test failures with the following patch, so if it's needed, perhaps a regression test would be useful.

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index b1aa610d43..64be087bf1 100644
    a b class ModelAdmin(BaseModelAdmin):  
    17871787            form = ModelForm(request.POST, request.FILES, instance=obj)
    17881788            formsets, inline_instances = self._create_formsets(
    17891789                request,
    1790                 form.instance if add else obj,
     1790                form.instance,
    17911791                change=not add,
    17921792            )
    17931793            form_validated = form.is_valid()
Note: See TracTickets for help on using tickets.
Back to Top