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 8057987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:12:40+00:00 2026-06-05T09:12:40+00:00

Base class looks like this (code comes from Django, but the question isn’t Django

  • 0

Base class looks like this (code comes from Django, but the question isn’t Django specific):

class BaseModelForm(BaseForm):
    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
                 initial=None, error_class=ErrorList, label_suffix=':',
                 empty_permitted=False, instance=None):

class ModelForm(BaseModelForm):
    __metaclass__ = ModelFormMetaclass

Derived class looks like this:

class MyForm(forms.ModelForm):
    def __init__(self, data=None, files=None, *args, **kwargs):
        super(EdocForm, self).__init__(data, files, *args, **kwargs)
        self.Meta.model.get_others(data, files, kwargs.get('instance', None))

If the coder passes instance as a kwargs this is all fine and should work, but what if they don’t do that and pass it in args instead? What is a sane way to extract instance when dealing with *args and **kwargs? Admittedly in this case the chances of instance being in args is small, but if it was the 3rd argument instead of the 5th (not counting self).

  • 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-06-05T09:12:41+00:00Added an answer on June 5, 2026 at 9:12 am

    If you insist on accepting *args, one way to handle this situation if getting instance matters to MyForm, is to explicitly include instance as a keyword argument to MyForm, add instance to kwargs, and then pass up kwargs.

    class MyForm(forms.ModelForm):
        def __init__(self, data=None, files=None, instance=None, *args, **kwargs):
            kwargs['instance'] = instance
            super(EdocForm, self).__init__(data, files, *args, **kwargs)
    

    Note that, if someone put instance as the third positional argument, they would be making a very explicit error, since instance is the last argument to BaseModelForm.

    However, a better way to handle this situation would be to specifically not allow additional positional arguments. E.g.:

    class MyForm(forms.ModelForm):
        def __init__(self, data=None, files=None, **kwargs):
            super(EdocForm, self).__init__(data, files, *args, **kwargs)
            self.Meta.model.get_others(data, files, kwargs.get('instance', None))
    

    That way, MyForm can only be called with up to 2 positional arguments. Any more and the Python interpreter will generate a TypeError: <func> takes exactly 2 arguments (# given).

    If you need to use the instance argument, you should explicitly include it in the keyword arguments to MyForm. Failing that, you at least should note it in the doc string to the function.

    If you’re subclassing BaseModelForm and it has a self.instance variable, you could access it directly via super. E.g. self.instance = super(EdocForm, self).instance. That way, you let the superclass handle whatever it needs to do with instance and grab the instance for yourself post-processing. (be careful about syntax with super…you need both the class and self)

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

Sidebar

Related Questions

I have a class hierarchy that looks like this: class Base<TElement> { public TElement
I have usercontrols which inherit a base class which looks like this: BasePage.cs: using
I have a repository pattern setup using NHibernate. The base class looks like this:
I have a base class that is a template that looks like this: template
I have code that looks like this: var baseClass = function() { // CODE
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
Please take a look at this code: template<class T> class A { class base
Consider this - a base class A, class B inheriting from A, class C
I've been using the Curiously recurring template pattern The general code looks like this:
In the project I'm working on now, we have base Entity class that looks

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.