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

  • Home
  • SEARCH
  • 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 4091168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:19:56+00:00 2026-05-20T19:19:56+00:00

I have dozens of Models, each with ONE associated ModelForm (whose Meta.model refers to

  • 0

I have dozens of Models, each with ONE associated ModelForm (whose Meta.model refers to the Model in question).

E.g.

class FooModel(Model):
    pass

class FooModelForm(ModelForm):
    class Meta:
        model = FooModel

# current approach using a classmethod
FooModelForm.insert_in_model()  # does cls.Meta.model.form = cls

So, obviously, it’s easy to find FooModel given FooModelForm. What I want is to know the best way to do the REVERSE: find FooModelForm when I am presented with FooModel or even the string “Foo”.

Assume only one ModelForm for each model, although solutions that return multiple are fine.

My current approach is to stash the model in the form class (as shown above), but I’m interested in knowing better approaches especially ones that could compute it centrally (without the final line above).

EDIT: I’ve reviewed things like Django: Display Generic ModelForm or predefined form but I believe this is a simpler question than those. The Django admin code must do something along the lines of what I seek. But get_model equivalent for ModelForms? suggests that might be voodoo and that it would be best to just do dict['Foo']=FooModelForm or its equivalent to keep track of the association explicitly. Seems repetitious.

  • 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-20T19:19:56+00:00Added an answer on May 20, 2026 at 7:19 pm

    If you have under 20 forms, sounds like mapping out a dictionary is the easiest way. Django does this kinda thing internally too.

    For ModelForms, django admin just creates them on the fly via modelform_factory, so there is no comparable method to get_model


    I do see, your method is bullet proof, but requires a line in ever model def.

    If you only have one ModelForm per model, you could potentially iterate through the ModelForm subclasses until you find your form.

    find FooModelForm when I am presented
    with FooModel or even the string
    “Foo”.

    modelforms = forms.ModelForm.__subclasses__()
    def get_modelform(model):
        try:
            return filter(lambda x:x.Meta.model == model, modelforms)[0] 
        except IndexError:
            print "apparently, there wasn't a ModelForm for your model"
    

    If you want to pull the ModelForm as a string, you’ll need to make sure both
    app_label and __name__ are correct, which means it will be easier to use get_model('app', 'model') in the function.

    You could combine this with your method and automatically place an attribute on your models that point to its ModelForm.

    Hook into the class_prepared signal at the top of your apps, find the corresponding ModelForm and attach it to your Model class.
    Hope that helps or gives you some ideas.

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

Sidebar

Related Questions

I have a class that I am binding to my view model. It is
I have dozens of functions which are alternatives to each other. They take an
I have the following models class Project < ActiveRecord::Base has_many :project_members has_many :members, :through
We have a system in which each thread (there can be dozens of them)
Given the Django models: class ContainerOwner(models.Model): id = models.IntegerField() class Container(models.Model): owner = models.ForeignKey(ContainerOwner)
I have just started with knockout this Model and viewmodel: $(function() { // Class
In my career I have seen literally dozens of ways that people choose to
I have a website with a two column layout, like there are dozens I
I'm writing some unit tests for one of my rails models. It's a large
I have a coding style question which probably should be asked of a senior

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.