form_valid method in django-extra-views. In reality form(s)_valid

独自空忆成欢 提交于 2019-12-13 20:28:23

问题


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:

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

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, ))

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.

How to do same stuff in CreateWithInlinesView?

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())



回答1:


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.

FINALY:

    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())


来源:https://stackoverflow.com/questions/55572161/form-valid-method-in-django-extra-views-in-reality-forms-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!