当前我有三个模型,如下列出的简化的模型定义
class Article(models.Model): title... content... class Attachment(models.Model): file... article = models.ForeignKey(Article, ref_name = 'attachments') class Photo(models.Model): image... article = models.ForeignKey(Article, ref_name = 'photos')
inlineformset_factory(parent_model,model,form=ModelForm, formset=BaseInlineFormSet,fk_name=None, fields=None,exclude=None, extra=3,can_order=False,can_delete=True,max_num=None, formfield_callback=None,widgets=None,validate_max=False)
AttachmentInlineFormset = inlineformset_factory(Article, Attachment, extra = 1) PhotoInlineFormset = inlineformset_factory(Article, Photo, extra = 1)
实例化inline formset:
attachment_formset = AttachmentInlineFormset(**self.get_form_kwargs(), prefix = 'attachments') photo_formset = PhotoInlineFormset(**self.get_form_kwargs(), prefix = 'photos')
在CreateView或者UpdateView中,使用
class XXView(CreateView): def form_valid(self, form): instance = form.save(commit = False) attachment_formset = AttachmentInlineFormset(instance = instance, prefix = 'attachments', **self.get_form_kwargs()) photo_formset = PhotoInlineFormset(instance = instance, prefix = 'photos', **self.get_form_kwargs()) if attachment_formset.is_valid() and photo_formset.is_valid(): instance = form.save() for fs in [attachment_formset, photo_formset]: fs.instance = instance fs.save() return HttpResponseRedirect(self.get_success_url()) else: return self.form_invalid(self, form)