Skip to content Skip to sidebar Skip to footer

How Do You Load A Custom Field In Django

note : This is closely related to the answer in this question : django admin - add custom form fields that are not part of the model In Django it is possible to create custom Model

Solution 1:

Override the form's __init__ method and set the initial property of the field:

class YourModelForm(forms.ModelForm):

    extra_field = forms.CharField()

    def __init__(self, *args, **kwargs):
        super(YourModelForm, self).__init__(*args, **kwargs)
        initial = '%s*rnd' % self.instance.pk if self.instance.pk else 'new'
        self.fields['extra_field'].initial = initial

Post a Comment for "How Do You Load A Custom Field In Django"