Answer the question
In order to leave comments, you need to log in
How to set initial parameter for ModelForm?
Good afternoon.
Tell me how to get the initial values of the form from the model?
class VendorEditForm(forms.ModelForm):
class Meta:
model = Vendor
fields = ('avatar','description',)
context['form'] = self.form_class(self.request.GET, initial=??? )
Answer the question
In order to leave comments, you need to log in
www.stackoverflow.com/a/4674127
# forms.py
# ...
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
# views.py
# ...
def my_view(request, id):
instance = get_object_or_404(MyModel, id=id)
form = MyForm(request.POST or None, instance=instance)
if form.is_valid():
form.save()
return redirect('next_view')
return direct_to_template(request, 'my_template.html', {'form': form}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question