Opened 7 years ago

Last modified 7 years ago

#28326 closed New feature

[new feature] — at Version 1

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 (1)

comment:1 by Luoxzhg, 7 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top