本文整理汇总了Python中contact.forms.ContactForm.save_m2m方法的典型用法代码示例。如果您正苦于以下问题:Python ContactForm.save_m2m方法的具体用法?Python ContactForm.save_m2m怎么用?Python ContactForm.save_m2m使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类contact.forms.ContactForm
的用法示例。
在下文中一共展示了ContactForm.save_m2m方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: contact_create_or_edit
# 需要导入模块: from contact.forms import ContactForm [as 别名]
# 或者: from contact.forms.ContactForm import save_m2m [as 别名]
def contact_create_or_edit(request, id=None):
if id:
title = _('Edit a contact')
contact = get_object_or_404(Contact, pk=id, owner=request.user)
address = contact.address
else:
title = _('Add a contact')
contact = None
address = None
PhoneNumberFormSet = inlineformset_factory(Contact,
PhoneNumber,
form=PhoneNumberForm,
fk_name="contact",
extra=1)
contacts = Contact.objects.filter(owner=request.user)
if contact:
contacts = contacts.exclude(pk=contact.id)
if request.method == 'POST':
contactForm = ContactForm(request.POST, instance=contact, prefix="contact")
contactForm.fields['contacts'].queryset = contacts
addressForm = AddressForm(request.POST, instance=address, prefix="address")
phonenumberformset = PhoneNumberFormSet(request.POST, instance=contact)
if contactForm.is_valid() and addressForm.is_valid() and phonenumberformset.is_valid():
user = request.user
address = addressForm.save(commit=False)
address.save(user=user)
contact = contactForm.save(commit=False)
contact.address = address
contact.save(user=user)
contactForm.save_m2m()
for phonenumberform in phonenumberformset.forms:
phonenumber = phonenumberform.save(commit=False)
if phonenumber.type:
phonenumber.contact = contact
phonenumber.save(user=user)
for deleted_phonenumberrowform in phonenumberformset.deleted_forms:
deleted_phonenumberrowform.cleaned_data['ownedobject_ptr'].delete()
messages.success(request, _('The contact has been saved successfully'))
return redirect(reverse('contact_detail', kwargs={'id': contact.id}))
else:
messages.error(request, _('Data provided are invalid'))
else:
contactForm = ContactForm(instance=contact, prefix="contact")
contactForm.fields['contacts'].queryset = contacts
addressForm = AddressForm(instance=address, prefix="address")
phonenumberformset = PhoneNumberFormSet(instance=contact)
return render_to_response('contact/edit.html',
{'active': 'contact',
'title': title,
'contactForm': contactForm,
'addressForm': addressForm,
'phonenumberformset': phonenumberformset},
context_instance=RequestContext(request))