1

I have a django model to define content pages, in which I had a field to choose if I wanted some form to appear or not within the page.

Now I've made a second form. I'd like to use one or another depending on some field filled in ContentPage instance.

To be more specific, I'm stuck with this part:

class ViewContentPage(CreateView):
    model = ContentPage
    # What I'd like to do
    form_class = depending_on_some_ContentPage_field()

    # From here, overriden methods and stuff

The problem is that it seems impossible to get access to kwargs or request at class level definition (in order to load the proper ContentPage record depending on slug extracted from kwargs, and then determine from it's field which form to load)

I've been thinking on different approaches to achieve a similar result, which research led me to find this question: Can I call a view from within another view?. I'm thinking that "making FormViews as a sub-view inside ViewContentPage" would pave the way, or a completely independent view for forms rendered on template level...

Can something like the exposed in pseudo_code above be achieved with some other kind of approach?

EDIT

As @Alasdair reccommended, I override get_form_class() method in my view. It looks like it does what I want, but a little failure which traceback is entirely in Django itself stands in my way.

I use Django v1.8.3, the point where the exception is raised is in django.core.handlers.base.py line 132 (first in the traceback). The exception is:

TypeError: 'instance' is an invalid keyword argument for this function

And the cause of this exception seems to be in django/db/models/base.py line 480 (last in the traceback). Somehow, this class (django.db.models.base.Model) is running even if self = Error in formatting: RelatedObjectDoesNotExist: MyModelForForm2 has no page. Really confusing.

It's not the first time I see an error which traceback doesn't stop anywhere in my source, and even so, correcting the source would eliminate the error. However, I am still stuck on how to correct this without modifying Django itself.

1
  • 1
    I would discourage looking at the 'sub-view' approach. Since you're using Python, it's possible to call a class based view from within another view, but I don't think I'd ever recommend this approach. The best way to customize the functionalities of class based views is by overriding the appropriate methods or attributes.
    – Alasdair
    Dec 18, 2015 at 13:20

1 Answer 1

1

You can override the get_form_class method. Inside it, you can access self.request and get the slug from self.kwargs.

class ViewContentPage(CreateView):
    model = ContentPage

    def get form_class(self):
        # return correct class based on self.kwargs and self.request
4
  • Good idea. I tried implementing it directly returning the desired form class depending on field, without any super() call. It triggers me TypeError: 'instance' is an invalid keyword argument for this function, and the traceback doesn't even stop in my source code. Thanks anyway, I think this is the way to go.
    – SebasSBM
    Dec 18, 2015 at 13:15
  • I can't tell why you are getting the TypeError without seeing the traceback. You shouldn't have to call super() in get_form_class.
    – Alasdair
    Dec 18, 2015 at 13:23
  • It's ok. It's just to not forget it. Somehow I'll find a way to prevent this error.
    – SebasSBM
    Dec 18, 2015 at 13:25
  • 1
    Jeez, almost 2 hours stuck because in get_form_class() I was returning the Model instances instead of the Forms! :-$ After finding this slip out, your suggestion works like a charm! Thank you very much for your help. BTW, it looks like updating my questions somehow is as effective as Rubber Duck Debugging... xD
    – SebasSBM
    Dec 21, 2015 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.