68
The ModelForm‘s save method returns the saved object.
Try this:
def contact_create(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
new_contact = form.save()
return HttpResponseRedirect(reverse(contact_details, args=(new_contact.pk,)))
else:
form = ContactForm()
13
In the case where you have set form.save(commit=False) because you want to modify data and you have a many-to-many relation, then the answer is a little bit different:
def contact_create(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
new_contact = form.save(commit=False)
new_contact.data1 = "gets modified"
new_contact.save()
form.save_m2m()
return HttpResponseRedirect(reverse(contact_details, args=(new_contact.pk,)))
else:
form = ContactFrom()
https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#the-save-method
- [Django]-Django authentication and Ajax – URLs that require login
- [Django]-CSS styling in Django forms
- [Django]-Can I make list_filter in django admin to only show referenced ForeignKeys?
Source:stackexchange.com