Creating a custom mixin for use with the generic CreateView

164 views
Skip to first unread message

Guruprasad L

unread,
Oct 4, 2012, 5:44:22 PM10/4/12
to django...@googlegroups.com
Hi I have created a Mixin that overrides just the get_form method, to be used with a class that inherits from CreateView. The Mixin inherits from object. In my view, when I include CreateView and my mixin in that order, the mixin code doesn't get executed. But when I swap the order of inheriting the classes, the mixin code works but the form processing of CreateView doesn't. What could be the issue here? The code is at http://dpaste.com/hold/809974/

I understand that this behaviour has to do with Python's MRO, which I came across just now. But I have very little knowledge of Django's generic views to understand why the form processing doesn't work when the mixin code works on changing the order of inheritance.

Thanks & Regards,
Guruprasad

Guruprasad L

unread,
Oct 4, 2012, 5:55:59 PM10/4/12
to django...@googlegroups.com
On Friday, October 5, 2012 3:14:22 AM UTC+5:30, Guruprasad L wrote:
Hi I have created a Mixin that overrides just the get_form method, to be used with a class that inherits from CreateView. The Mixin inherits from object. In my view, when I include CreateView and my mixin in that order, the mixin code doesn't get executed. But when I swap the order of inheriting the classes, the mixin code works but the form processing of CreateView doesn't. What could be the issue here? The code is at http://dpaste.com/hold/809974/

I understand that this behaviour has to do with Python's MRO, which I came across just now. But I have very little knowledge of Django's generic views to understand why the form processing doesn't work when the mixin code works on changing the order of inheritance.


Ok, I found out the issue in the code. The mixin wasn't calling super and hence breaking the hierarchy. On adding it, the code works. This is the first time I am using mixins in code. I read mixed opinions about mixins, a few of them saying that it is bad. If it is bad, is there some other way to implement this?

Thanks & Regards,
Guruprasad

Javier Guerra Giraldez

unread,
Oct 4, 2012, 6:24:54 PM10/4/12
to django...@googlegroups.com
On Thu, Oct 4, 2012 at 4:55 PM, Guruprasad L <lgp1...@gmail.com> wrote:
> This is the first time I am using mixins in code. I read mixed opinions
> about mixins, a few of them saying that it is bad. If it is bad, is there
> some other way to implement this?

regardless of any moral issues for or against mixins, this is how
class based views are currently designed: you can subclass to add
concrete functionality once, or create a mixin to apply some
functionality to several concrete subclasses.

so, no need to feel bad :-)

(besides, some people just love mixins)

--
Javier

Guruprasad L

unread,
Oct 5, 2012, 3:57:12 PM10/5/12
to django...@googlegroups.com
Hi,


On Friday, October 5, 2012 3:55:26 AM UTC+5:30, Javier Guerra wrote:
On Thu, Oct 4, 2012 at 4:55 PM, Guruprasad L <lgp1...@gmail.com> wrote:
> This is the first time I am using mixins in code. I read mixed opinions
> about mixins, a few of them saying that it is bad. If it is bad, is there
> some other way to implement this?

regardless of any moral issues for or against mixins, this is how
class based views are currently designed:  you can subclass to add
concrete functionality once, or create a mixin to apply some
functionality to several concrete subclasses.

Thank you for your feedback. At least now I don't feel bad for creating a mixin and using it. Since the method I wanted to override (get_form() of CreateView) was already having a hierarchy of super() calls, it worked fine. But assuming I want to add functionality to a function that doesn't require calling super()'s function of the same name, like say form_valid() is it possible to use it in the same mixin?

This is what I want to achieve:

While using CreateView, when the models have a user foreignkey field, I don't want to give the foreign key choice to the user. So I remove the user field from the form by overriding the get_form() method in my mixin. When I inherit from the mixin, I get that functionality added to all subclasses of CreateView that I have. And to add the user foreign key reference before saving, I add it by overriding the form_valid() method and set it to the current user before saving it to the database.

I already have the mixin that overrides the get_form() method. In the method, it calls the super() get_form() and gets the form and removes the field and returns the form. But in form_valid() function, if I override it, I am not explicitly calling any super() function. I just add the field, save the modelform instance and redirect. So is it possible to override the form_valid() with a mixin even though there is no explicit invocation of super() methods?

Thanks & Regards,
Guruprasad
Reply all
Reply to author
Forward
0 new messages