具有多个内联表单的基于Django类的CreateView和UpdateView

具有多个内联表单的基于Django类的CreateView和UpdateView,第1张

概述我一直在尝试使用多个内联表单来执行基于Django类的CreateView和UpdateView CreateView工作正常,但UpdateView无法正常工作,如果任何人尝试使用多个内联表单的UpdateView,任何人都尝试过共享更新视图代码段. # models.pyfrom django.db import modelsclass Recipe(models.Model): 我一直在尝试使用多个内联表单来执行基于Django类的CreateVIEw和UpdateVIEw

CreateVIEw工作正常,但UpdateVIEw无法正常工作,如果任何人尝试使用多个内联表单的UpdateVIEw,任何人都尝试过共享更新视图代码段.

# models.pyfrom django.db import modelsclass Recipe(models.Model):    Title = models.CharFIEld(max_length=255)    description = models.TextFIEld()class IngredIEnt(models.Model):    recipe = models.ForeignKey(Recipe)    description = models.CharFIEld(max_length=255)class Instruction(models.Model):    recipe = models.ForeignKey(Recipe)    number = models.PositiveSmallintegerFIEld()    description = models.TextFIEld()# forms.pyfrom django.forms import ModelFormfrom django.forms.models import inlineformset_factoryfrom .models import Recipe,IngredIEnt,Instructionclass RecipeForm(ModelForm):    class Meta:        model = RecipeIngredIEntFormSet = inlineformset_factory(Recipe,extra=0)InstructionFormSet = inlineformset_factory(Recipe,Instruction,extra=0)# vIEws.pyfrom django.http import httpResponseRedirectfrom django.vIEws.generic.edit import CreateVIEw,UpdateVIEwfrom django.shortcuts import get_object_or_404from .forms import IngredIEntFormSet,InstructionFormSet,RecipeFormfrom .models import Recipeclass RecipeCreateVIEw(CreateVIEw):    template_name = 'recipe_add.HTML'    model = Recipe    form_class = RecipeForm    success_url = '/account/dashboard/'    def get(self,request,*args,**kwargs):        self.object = None        form_class = self.get_form_class()        form = self.get_form(form_class)        ingredIEnt_form = IngredIEntFormSet()        instruction_form = InstructionFormSet()        return self.render_to_response(            self.get_context_data(form=form,ingredIEnt_form=ingredIEnt_form,instruction_form=instruction_form))    def post(self,**kwargs):        self.object = None        form_class = self.get_form_class()        form = self.get_form(form_class)        ingredIEnt_form = IngredIEntFormSet(self.request.POST)        instruction_form = InstructionFormSet(self.request.POST)        if (form.is_valID() and ingredIEnt_form.is_valID() and            instruction_form.is_valID()):            return self.form_valID(form,ingredIEnt_form,instruction_form)        else:            return self.form_invalID(form,instruction_form)    def form_valID(self,form,instruction_form):        self.object = form.save()        ingredIEnt_form.instance = self.object        ingredIEnt_form.save()        instruction_form.instance = self.object        instruction_form.save()        return httpResponseRedirect(self.get_success_url())    def form_invalID(self,instruction_form):        return self.render_to_response(            self.get_context_data(form=form,instruction_form=instruction_form))class RecipeUpdateVIEw(UpdateVIEw):    template_name = 'recipe_add.HTML'    model = Recipe    form_class = RecipeForm    def get_success_url(self):        self.success_url = '/account/dashboard/'        return self.success_url    def get_context_data(self,**kwargs):        context = super(RecipeUpdateVIEw,self).get_context_data(**kwargs)        if self.request.POST:            context['form'] = RecipeForm(self.request.POST,instance=self.object)            context['ingredIEnt_form'] = IngredIEntFormSet(self.request.POST,instance=self.object)            context['instruction_form'] = InstructionFormSet(self.request.POST,instance=self.object)        else:            context['form'] = RecipeForm(instance=self.object)            context['ingredIEnt_form'] = IngredIEntFormSet(instance=self.object)            context['instruction_form'] = InstructionFormSet(instance=self.object)        return context    def post(self,instruction_form=instruction_form))

提前致谢.

解决方法 我的猜测是你不能做
self.object = None

在UpdateVIEw中覆盖的post方法.所以,尝试

self.object = self.get_object()

相反,一旦你在这种情况下已经有一个对象实例.

总结

以上是内存溢出为你收集整理的具有多个内联表单的基于Django类的CreateView和UpdateView全部内容,希望文章能够帮你解决具有多个内联表单的基于Django类的CreateView和UpdateView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099719.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存