0

I'm trying to find a way on how to write one FormView that can handle different ModelForms.

My situation:

I have 6 models that have a common AbstractBaseClass with a custom ModelManager. I want to display them as ModelForms in a FormView that displays the form(s) and handles the POST data. They have the same methods.

My approach:

Abstract Base Class with a custom ModelManager that all models inherit from.

**models.py**    
class AbstractBaseClass():
  slug = models.CharField(max_length=20)
  name = models.CharField(max_length=40)

  objects = MyModelManager()

  def __str__(self):
    return self.name

def get_absolute_url(self):
    return reverse('some-name', kwargs=[str(self.name)])

  class Meta:
    abstract = True


class ChildClassOne(AbstractBaseClass):
  city = models.CharField(...)

class ChildClassTwo(AbstractBaseClass):
  city = models.CharField(...)

In forms.py I created the corresponding ModelForms ChildClassOneForm and ChildClassTwoForm with validation-methods.

My views.py is something like this:

**views.py**
class ChildClassOneView(FormView):
    template_name = 'formulare/formular_detail.html'
    form_class = ChildClassOneForm     #here's the problem - it should work for other ModelForms, too

    def post(self, request, *args, **kwargs)
         form = self.form_class(request.POST)
         if form.is_valid():
             # do lots of different stuff with the data, send email, etc. 

**I want to avoid:**
class ChildClassTwoView(FormView):
   form_class = ChildClassTwoForm
   # the rest is identical with ChildClassOneView

**urls.py** 
path('forms/<str:slug>', ChildClassOneView.as_view(), name='some-name')

And here's my question:

How can I write a FormView that can be used for ChildClassOneForm AND ChildClassTwoForm? How can I pass more than one form to the FormView? Or in other words: How do I not repeat myself over and over again?

Thanks a lot for your ideas, it's much appreciated!

  • Yes, You can definitely handle multiple forms in one view. BUT i haven't worked with Class Based view BUT i can tell you in function based views. – PrOgRaMmEr Jan 27 '21 at 16:18
  • Thanks, @Progam. I'm open to any solution! Let's hear it! – Katharina Jan 27 '21 at 16:32
  • is [this](https://stackoverflow.com/a/1395866/4225972) maybe what you are looking for? – xtlc Jan 27 '21 at 16:38
  • Thanks, @xtlc. This is not what I'm looking for. I don't want to handle multiple forms in one view. Instead, I want to create a FormView that displays a view either for Form1 OR Form2 - depending on what form was requested by the user. – Katharina Jan 27 '21 at 16:42

1 Answers1

0

You can do it like :-

views.py

def child_class(request):

    if request.user.is_authenticated:
        form_1 = ChildClassOneForm(data=request.POST,instance=request.user)
        form_2 = ChildClassTwoForm(data=request.POST,instance=request.user)

    else:
        form_1 = ChildClassOneForm()
        form_2 = ChildClassTwoForm()

    context = {
        'form_1': form_1,
        'form_2': form_2,
     }
    return render(request, 'formulare/formular_detail.html', context)

formulare/formular_detail.html

You can access each forms in Template by form_1 and form_2.

For Form 1 :-

        {{ form_1 }}

For Form 2 :-

        {{ form _2 }}

Note : This is only for Function Based Views.

PrOgRaMmEr
  • 872
  • 1
  • 2
  • 18
  • Thanks a lot, @Progam! Just tried a quick and dirty version - seems to work well for my issue. I will dive into it deeper tomorrow and give more feedback. Appreciate it! – Katharina Jan 27 '21 at 17:14
  • Your solution works well for my purpose - thanks a lot, @Progam! I'm still interested in whether you can use FormView with multiple forms. If anyone has an idea, I'm all ears. – Katharina Jan 28 '21 at 14:59