在Django中保存一个ModelForm后获取主键

时间:2022-03-26 19:16:58

How do I get the primary key after saving a ModelForm? After the form has been validated and saved, I would like to redirect the user to the contact_details view which requires the primary key of the contact.

如何在保存一个ModelForm后获得主键?在验证并保存表单之后,我希望将用户重定向到contact_details视图,该视图需要联系人的主键。

def contact_create(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse(contact_details, args=(form.pk,)))
    else:
        form = ContactForm()

2 个解决方案

#1


47  

The ModelForm's save method returns the saved object.

ModelForm的保存方法返回所保存的对象。

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

#2


9  

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:

如果您已经设置了form.save(commit=False),因为您想修改数据,并且您有一个多对多的关系,那么答案会有一点不同:

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

https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/保存方法

#1


47  

The ModelForm's save method returns the saved object.

ModelForm的保存方法返回所保存的对象。

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

#2


9  

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:

如果您已经设置了form.save(commit=False),因为您想修改数据,并且您有一个多对多的关系,那么答案会有一点不同:

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

https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/保存方法