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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:42:11+00:00 2026-05-29T21:42:11+00:00

I want to create a view that is able to show a ModelForm for

  • 0

I want to create a view that is able to show a ModelForm for various different models. It does this by obtaining the content type of the model and then dynamically instantiating the model form associated with that particular model. Here is my model:

from django.db import models

class SomeModel(models.Model):
    name = models.CharField(max_length=150)

    def __unicode__(self):
        return self.name

And inside the same app there is a forms.py with the following form:

from django.forms import ModelForm

from someapp.models import SomeModel 

class SomeModelForm(ModelForm):
    class Meta:
        model = SomeModel
        fields = ('name',)

So what I want to do inside of my view file is return the correct form for each model dynamically. I tried the following:

from django.db import models
from someapp.forms import SomeModelForm

    class SomeModel(models.Model):
        name = models.CharField(max_length=150)

        form = SomeModelForm

        def __unicode__(self):
            return self.name

But it doesn’t work because of the obvious circular import. Does anyone have any idea how I might go about achieving this? I tried toying with modelform_factory, but it seems to ignore any of my custom model forms in forms.py.

EDIT: I should of mentioned that I won’t have an instance of the model, just the model class itself, so having a method that inside of the model doesn’t work (it does, however, work if you are calling it on an instance of the model)

  • 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-29T21:42:12+00:00Added an answer on May 29, 2026 at 9:42 pm

    You could get around the circular import by importing your model form inside a method.

    class SomeModel(models.Model):
        name = models.CharField(max_length=150)
    
        @staticmethod
        def get_form_class():
            from someapp.forms import SomeModelForm
            return SomeModelForm
    
    # in your view:
    SomeModel.get_form_class()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a view that has different displays according to the role
I want to create a view that consists solely of a UITextView . When
I want to create a view that contains two forms with their submit buttons.
I want to create a tabbed view of pages similar to that of the
I want to create a view from the following two tables: Entry: entry_id entry_date
I want to create a view in a one-to-many relation. Here are my relations:
I want to create a custom alert view within my iOS application. For example,
I want to create 2 separate pickers in the same view using the same
Sorry for the Newb-ness. I want to create a list of view elements in
I want to create tabbed ActionBar (NAVIGATION_MODE_TABS) with custom view (DISPLAY_SHOW_CUSTOM). It should 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.