django-extra-views中的form_valid方法。现实形式_有效 [英] form_valid method in django-extra-views. In reality form(s)_valid
问题描述
我正在尝试使用Django Extra Views包基于模型+内联表单集+ USER模型的其他信息来创建新条目。我知道如何通过基于函数的视图来执行此操作,但是现在尝试减少代码量:
I am trying to use Django Extra Views pack to create new entry based on model + inline formset + extra information from the USER model. I know how to do it via function based views but now trying to decrease amount of the code:
我有2个模型+用户模型:
I have 2 models + user model:
Model1: # primary model
author = models.ForeignKey("ExtraUser", )
+some fileds
Model2 # secondary model
photo = models.ForeignKey("Model1", )
+ some fields
# user model
Model ExtraUser(AbstractBaseUser)
+ some fileds
我在VIEW后面使用渲染并保存在一起:
I use following VIEW to render and save it all together:
class ItemInline(InlineFormSetFactory):
model = Model2
fields = ["somefiled"]
class CreateBoatView(SuccessMessageMixin, LoginRequiredMixin, CreateWithInlinesView):
model = Model1
inlines = [ItemInline]
fields = ["list of the fields here"]
template_name = 'create.html'
def get_success_url(self):
return reverse('app:url', args=(self.object.pk, ))
除1件事外,所有其他功能均有效:我无法指定当前用户为条目作者,即author = models.ForeignKey( ExtraUser,)始终为NULL
It all work except 1 thing: I cant appoint current user as an entry author, that is author = models.ForeignKey("ExtraUser", ) is always NULL
在基于祖先函数的视图中,我曾经做过以下事情:
in ancestor function based view I used to do following:
if form1.is_valid():
prim = form1.save(commit=False)
prim.author = request.user # that is I connect this entry to the current user.
# + do something + save(commit=True) finally.
如何在CreateWithInlinesView中做同样的事情?
How to do same stuff in CreateWithInlinesView?
尝试了以下内容。 Doenst工作
tried following. Doenst work
def dispatch(self, request, *args, **kwargs):
self.user = request.user
return CreateWithInlinesView.dispatch(self, request, *args, **kwargs)
def form_valid(self, form): #(self, form, inlines)??
self.object = form.save(commit=False)
self.object.author = self.request.user
self.object.save()
return HttpResponseRedirect(self.get_success_url())
# super-class form_valid method (for reference)
def forms_valid(self, form, inlines):
"""
If the form and formsets are valid, save the associated models.
"""
self.object = form.save()
for formset in inlines:
formset.save()
return HttpResponseRedirect(self.get_success_url())
推荐答案
非常感谢作者Django Extra Views,因为它们添加了特殊的方法,而不是form_valid...。
猜测它如何调用?
forms_valid。您确实需要几秒钟来获得差异,对吧?
对我来说大约需要5个小时才能找到答案。
Well, thanks a lot to authors of the Django Extra Views , as they have added special method instead of the form_valid.... guess how it called??? forms_valid . You do need few seconds to get the difference, right?? for me it took around 5 hours to find it out.
最终:
def forms_valid(self, form, inlines): #yes, f%%ng form(s)_valid, yeh...
"""
If the form and formsets are valid, save the associated models.
"""
self.object = form.save(commit=False)
self.object.author = self.request.user
form.save(commit=True)
for formset in inlines:
formset.save()
return HttpResponseRedirect(self.get_success_url())
这篇关于django-extra-views中的form_valid方法。现实形式_有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!