Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28326 closed New feature (wontfix)

Allow providing initial data in ModelForm Meta

Reported by: Luoxzhg Owned by: nobody
Component: Forms Version: 1.11
Severity: Normal Keywords: ModelForm, Meta, initials, modelform_factory
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Luoxzhg)

Now, we can specify 'widgets', 'labels', and so on in ModelForm's Meta innerclass and modelform_factory. But We can't specify 'initials' in Meta and modelform_factory. We must provide initial values in explicitly defined fields or in ModelForm instantiation.

class Person(models.Model):
    first = models.CharField(max_length=10)
    last = models.CharField(max_length=10)

class PersonForm(forms.ModelForm):
    last = forms.CharField(max_length=10, required=True, initial="Smith")
    class Meta:
        model = Person
        fields = '__all__'

If we can specify initial values in Meta, it is easier to specify initial values:

class PersonForm(forms.ModelForm):
    class Meta:
         model = Person
         fields = '__all__'
         initials = {'last': 'Smith'}

Change History (3)

comment:1 by Luoxzhg, 7 years ago

Description: modified (diff)

comment:2 by Tim Graham, 7 years ago

Resolution: wontfix
Status: newclosed
Summary: [new feature]Allow providing intial data in ModelForm Meta

We aren't going to add more and more override options in Meta. The decision from #20000: "It's understood that we have to draw a line somewhere, else we would have field definitions in the Meta, but I believe that line should be drawn at "anything cosmetic" available through Meta overrides, and "anything functional" through fields overrides."

comment:3 by Tim Graham, 7 years ago

Summary: Allow providing intial data in ModelForm MetaAllow providing initial data in ModelForm Meta
Note: See TracTickets for help on using tickets.
Back to Top