I understand that you can use the parameter initiatefor the class Formfrom this question .
I am creating an editing form and I am trying to figure out how to initiate values from a pre-existing object.
Am I doing this at the template level or at the presentation level (I don’t even know how to do this at the template level)? Or maybe I need to pass the actual object to the form and start at the form level?
What is the best practice?
EDIT:
For @Bento: in my original FormI am doing something like this
class OrderDetailForm(forms.Form):
work_type = forms.ChoiceField(choices=Order.WORK_TYPE_CHOICES)
note = forms.CharField(widget=forms.Textarea)
def __init__(self, creator_list=None, place_list=None, *args, **kwargs):
super(OrderCreateForm, self).__init__(*args, **kwargs)
if creator_list:
self.fields['creator'] = UserModelChoiceField(
queryset=creator_list,
empty_label="Select a user",
)
def clean(self):
super(OrderCreateForm, self).clean()
if 'note' in self.cleaned_data:
if len(self.cleaned_data['note']) < 50:
self._errors['note'] = self.error_class([u"Please enter a longer note."])
del self.cleaned_data['note']
return self.cleaned_data
How can I do this with ModelForm?
source
share