5

I am working on a Django application but this seems like it is just a python question, with nothing necessarily specific to Django. I'm pretty new to python, and its hard to describe what I am trying to do, but easier to show so here goes:

I have one class:

class SlideForm(ModelForm):

    class Meta:
        model = Slide

which I subclass:

class HiddenSlideForm(SlideForm):
    def __init__(self, *args, **kwargs):
        super(HiddenSlideForm, self).__init__(*args, **kwargs)
        for name, field in self.fields.iteritems():
            field.widget = field.hidden_widget()
            field.required = False

and then I have another class:

class DeckForm(ModelForm):     

    def __init__(self, *args, **kwargs):
        # do some stuff here
        return super(DeckForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Deck
        # other stuff here  

which I also sub-class:

class HiddenDeckForm(DeckForm):

    def __init__(self, *args, **kwargs):
        super(HiddenDeckForm, self).__init__(*args, **kwargs)
        for name, field in self.fields.iteritems():
            field.widget = field.hidden_widget()
            field.required = False

Note that the subclasses have the exact same code other than class names and do the exact same thing. I have been trying to figure what the best way to genericize this so I can keep it DRY and easily use it for other classes, and have considered decorators and/or multiple inheritance--both of which are new concepts for me--but I keep getting mixed up.

Help is appreciated!

(As a side note, feel free to point out any problems you see in my django code :) )

B Robster
  • 40,605
  • 21
  • 89
  • 122
  • Question should perhaps ask for a way to solve the problem so it doesn't involve embedding lots of hidden fields in the form? – John Mee May 19 '11 at 01:55
  • Thanks for the suggestion, John. Posted a separate question that describes the scenario where I am using this. http://stackoverflow.com/questions/6054124 . Feel free to post an answer if you have some ideas. I'd love to figure out how to implement this in a cleaner fashion. – B Robster May 19 '11 at 05:23
  • +1 for finding a bug in my answer; Now I know more about python too! – SingleNegationElimination May 20 '11 at 00:59

2 Answers2

4

One option is to use a Mixin class; example:

First, the common behavior goes in the mixin:

class SomeMixin(object):
    def __init__(self, *args, **kwargs):
        super(SomeMixin, self).__init__(*args, **kwargs)
        for name, field in self.fields.iteritems():
            field.widget = field.hidden_widget()
            field.required = False

To the extent that you are in reasonable control of all of the classes in the inheritance graph, and so long as you call super in every method that needs to be overridden, then it doesn't matter too much what the derived classes look like.

However, you run into a problem when one of the superclasses does not itself call super at the correct time. It's very important that the overridden method, in that case, must be called last, since once it's called, no more calls will be made.

The simplest solution is to make sure that each class actually derives from the offending superclass, but in some cases, that's just not possible; deriving a new class creates a new object that you don't actually want to exist! Another reason might be because the logical base class is too far up the inheritance tree to work out. \

In that case, you need to pay particular attention to the order in which base classes are listed. Python will consider the left-most superclass first, unless a more derived class is present in the inheritance diagram. This is an involved topic, and to understand what python is really up to, you should read about the C3 MRO algorithm present in python 2.3 and later.

Base classes as before, but since all of the common code comes from the mixin, the derived classes become trivial

class HiddenSlideForm(SomeMixin, SlideForm):
    pass

class HiddenDeckForm(SomeMixin, DeckForm):
    pass

Note that the mixin class appears first, since we can't control what the *Form classes do in their init methods.

If the __init__ methods of either are non-trivial, you still get a win.

class HiddenSlideForm(SomeMixin, SlideForm):
    def __init__(self, *args, **kwargs):
        super(HiddenSlideForm, self).__init__(*args, **kwargs)
        do_something_special()

Make sure that object is in the inheritance diagram, somewhere. Strange things can happen otherwise.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • 2
    Thanks for the reply! I do inherit from Django supplied base classes (ModelForm), but your code doesn't work for me. The init function in the Mixin never gets called. Here is a simple test case demonstrating what happens: http://pastebin.com/a1tnXsjA . Any idea what the fix is? (btw I'm on Python 2.6) – B Robster May 19 '11 at 07:17
  • Instead of using super, just call `__init__()` of `SomeMixin` and `SlideForm` explicitly (one after another) in `HiddenSlideForm.__init__()`. – Imran May 20 '11 at 07:11
  • Imram, while this isn't ideal, since I will have to write an init in every child class, it looks like it may be the way to do it, since the other suggested ideas for automating the override with a Mixin don't seem to work. If you create an answer-form version of your comment, I'll mark it as accepted, since it actually works. – B Robster May 23 '11 at 14:28
  • 1
    @Ben: See my answer(s) to your related question [Are Mixin class __init__ functions not automatically called in python?](http://stackoverflow.com/questions/6098970/are-mixin-class-init-functions-not-automatically-called-in-python/6100595#6100595). – martineau May 24 '11 at 17:40
0

Multiple inheritance (specifically, Mixins) would probably be the best solution here.

Example:

class HiddenFormMixin(object):
    def __init__(self, *args, **kwargs):
        for name, field in self.fields.iteritems():
            field.widget = field.hidden_widget()
            field.required = False


class SlideForm(ModelForm):
    class Meta:
        model = Slide


class HiddenSlideForm(SlideForm, HiddenFormMixin):
    pass


class DeckForm(ModelForm):     
    def __init__(self, *args, **kwargs):
        # do some stuff here
        return super(DeckForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Deck
        # other stuff here  


class HiddenDeckForm(DeckForm, HiddenFormMixin):
    pass

Do note that this might not work directly if you overwrite __init__ in both classes. In that case you could do something like this to specify the order:

class HiddenDeckForm(DeckForm, HiddenFormMixin):
    def __init__(self, *args, **kwargs):
        # do some stuff here
        DeckForm.__init__(self, *args, **kwargs)
        HiddenFormMixin.__init__(self, *args, **kwargs)
intuited
  • 23,174
  • 7
  • 66
  • 88
Wolph
  • 78,177
  • 11
  • 137
  • 148