Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#13918 closed (fixed)

[patch] Simplify implementation of `get_model_and_form_class`

Reported by: suzaku Owned by: suzaku
Component: Generic views Version: 1.2
Severity: Keywords:
Cc: liaoxuecong@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


In django.views.generic.create_update.get_model_and_form_class, a form_class is constructed this way

        # The inner Meta class fails if model = model is used for some reason.
        tmp_model = model
        # TODO: we should be able to construct a ModelForm without creating
        # and passing in a temporary inner class.
        class Meta:
            model = tmp_model
        class_name = model.__name__ + 'Form'
        form_class = ModelFormMetaclass(class_name, (ModelForm,), {'Meta': Meta})

When I looked into the django.forms.models module I found a function called modelform_factory that can make this much simpler:

form_class = modelform_factory(model)

Attachments (1)

13918_simpler_get_model_form.diff (1.2 KB) - added by suzaku 6 years ago.

Download all attachments as: .zip

Change History (4)

Changed 6 years ago by suzaku

comment:1 Changed 6 years ago by mk

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Even though there's an upcoming refactoring of generic views using class-based views I think this patch makes sense, if only to avoid repetition.

comment:2 Changed 6 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

Function-based generic views were deprecated by the introduction of class-based views in [14254]. Class-based views should solve this problem.

comment:3 Changed 5 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top