Thursday, November 3, 2011

Re: Abstract FormView

All you have to do is create a MyFormMixin class and add all those methods you want to override. Usually you will want to call super() on the class so that the normal behavior is also present in your final class.

The difference between a Mixin and a full-fledged Class can be nearly non-existent in some cases, so don't worry about the terminology. If you want, you can even subclass 'object' to make it as generic as it gets.


# example.py

class MyFormMixin(object):
    def get_form_kwargs(self, *args, **kwargs):
        kw = super(MyFormMixin, self).get_form_kwargs(*args, **kwargs)
        kw.update({'some': 'extra', 'permantent': 'kwargs'})
        return kw

class MyViewWithMixin(MyFormMixin, FormView):
    pass

# end example.py

Hope that helps! Let us know if you have any other questions.


Cheers,
AT


On Wed, Nov 2, 2011 at 6:47 PM, Kurtis <kurtis.mullins@gmail.com> wrote:
Hey,

I have a couple of FormViews that override quite a few methods. I want
to write an "Abstract View" that I can subclass for these. I'm
guessing what I actually need is a custom Mixin but I'm really not
sure.

Any suggestions on how to go about doing this?

Some methods I'm overriding:

get_form_kwargs
form_valid
form_invalid
get_context_data

Other than the form_class, the code in each is the same line for line
(except of course when referencing the Class name)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment