0

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


inheritance diagram

Aleksei Khatkevich
  • 1,923
  • 2
  • 10
  • 27

1 Answers1

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.

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())
Aleksei Khatkevich
  • 1,923
  • 2
  • 10
  • 27
  • thanks for the answer, it works. What happens if 1 of the many formset fails? Can we return form_invalid if any of them are invalid? – shawnngtq May 03 '21 at 01:06