Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5929735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:27:40+00:00 2026-05-22T14:27:40+00:00

I am working on a Django application but this seems like it is just

  • 0

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 🙂 )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:27:41+00:00Added an answer on May 22, 2026 at 2:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a web application using Python (Django) and would like to
I've just started building a prototype application in Django. I started out by working
I am relatively new to this, but I'm working through things. I want to
I am currently working on a Django/Pinax application (I'm sure my question is not
I've just plugged this old JSONField model snippet into my django application. It looks
I'm currently working on a social web application using python/django. Recently I heard about
I am working on an django application that will return what historically was a
I am working on a Django application which allows a user to upload files.
I'm running CentOS 5, and am trying to get a django application working with
I'm working on my first Django application. In short, what it needs to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.