Ticket #30407: modelform_reverse_initial.diff

File modelform_reverse_initial.diff, 1.2 KB (added by Alper Cugun, 12 months ago)

draft patch

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 5edbbd376f..2f08747091 100644
    a b class BaseModelForm(BaseForm): 
    281281        opts = self._meta
    282282        if opts.model is None:
    283283            raise ValueError('ModelForm has no model class specified.')
     284
     285        object_data = {}
     286
     287        if initial is not None:
     288            object_data.update(initial)
     289
    284290        if instance is None:
    285291            # if we didn't get an instance, instantiate a new one
    286292            self.instance = opts.model()
    287             object_data = {}
    288293        else:
    289294            self.instance = instance
    290             object_data = model_to_dict(instance, opts.fields, opts.exclude)
    291         # if initial was provided, it should override the values from instance
    292         if initial is not None:
    293             object_data.update(initial)
     295            object_data.update(model_to_dict(instance, opts.fields, opts.exclude))
     296
    294297        # self._validate_unique will be set to True by BaseModelForm.clean().
    295298        # It is False by default so overriding self.clean() and failing to call
    296299        # super will stop validate_unique from being called.
Back to Top