当前位置: 首页>>代码示例>>Python>>正文


Python ContactForm.save_m2m方法代码示例

本文整理汇总了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))
开发者ID:fouda2009,项目名称:aemanager,代码行数:62,代码来源:views.py


注:本文中的contact.forms.ContactForm.save_m2m方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。