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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:14:56+00:00 2026-05-31T11:14:56+00:00

I have a model with a get_form method. # models.py from model_utils.managers import InheritanceManager

  • 0

I have a model with a get_form method.

# models.py

from model_utils.managers import InheritanceManager
from breathingactivities.forms import ParentRecordForm, ChildRecordForm


class ParentRecord(models.Model):
    ....
    objects = InheritanceManager()

    def get_fields(self, exclude=('user')):
        fields = self._meta.fields
        return [(f.verbose_name, f.value_to_string(self)) for f in fields if not exclude.__contains__(f.name)]

    @classmethod
    def get_form(self, *args, **kwargs):
    return ParentRecordForm

class ChildRecord(ParentRecord):
    ....
    duration = DurationField(
    _('Duration'),
    help_text=_('placeholder'))

    @classmethod
    def get_form(self, *args, **kwargs):
        return ChildRecordForm

I have a view that uses this get_form method to determine to correct form for a given object.

# views.py

class ParentRecordUpdateView(UpdateView):
    model = ParentRecord
    form_class = ParentRecordForm
    template_name = 'parentrecords/create.html'

    def get_object(self, **kwargs):
        return ParentRecord.objects.get_subclass(slug=self.kwargs['slug'])

    def get_form_class(self, *args, **kwargs):
        form_class = self.model.objects.get_subclass(slug=self.kwargs['slug']).get_form()
        return form_class

    def get_form_kwargs(self):
        kwargs = super(ParentRecordUpdateView, self).get_form_kwargs()
        kwargs.update({'user': self.request.user})
        return kwargs

I am using InheritanceManager from django-model-utils so I get a clean API to subclasses when I query a parent class – that is the get_subclass() stuff, and this is why my view works with ParentRecord.

All this works good. I see via console that indeed form_class is the form class I’d expect, for example, ChildRecordForm when the instance is of ChildRecord.

In my forms.py, I can’t just import models.ParentRecord and models.ChildRecord, as I import those forms in my models.py, and thus an error is raised that Django can’t import these models. I presume because of circular imports.

So, I try this instead:

# forms.py

from django.db.models import get_model

class ParentRecordForm(forms.ModelForm):
    def __init__(self, user, *args, **kwargs):
        super (ParentRecordForm, self).__init__(*args, **kwargs)

    class Meta:
        model = get_model('model', 'ParentRecord')
        exclude = ('user')

class ChildRecordForm(forms.ModelForm):
    def __init__(self, user, *args, **kwargs):
        super (ChildRecordForm, self).__init__(*args, **kwargs)

    class Meta:
        model = get_model('model', 'ParentRecord')
        exclude = ('user')

However, model for these forms always returns None.

I I go and pass ChildRecordForm some totally unrelated model that I can import from a different app in my project, for example:

# forms.py

from another_app import AnotherModel

class ChildRecordForm(forms.ModelForm):
    def __init__(self, user, *args, **kwargs):
        super (ChildRecordForm, self).__init__(*args, **kwargs)

    class Meta:
        model = AnotherModel
        exclude = ('user')

Then it works, meaning, The form returns the fields of AnotherModel.

So, I can’t work out why get_model() works for me in shell, but not in a form class when I use it to declare a value for a 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-31T11:14:57+00:00Added an answer on May 31, 2026 at 11:14 am

    My guess on get_model() is that running it at the class-definition level can give incorrect results, since it will get evaulated as Django is populating all the models in it’s AppCache. My quick reading of the django.db.models.loading module doesn’t seem to show that issue, but one thing to try is to run get_model() inside a view and print out the results to see if it is what you think it should be, since by that time the AppCache should be fully loaded.

    But – as a workaround to get around the original circular import (so you don’t have to use get_model anyway) is to not do the form imports at the module level – you can stick them in the classmethod instead:

    class ParentRecord(models.Model):
        @classmethod
        def get_form(self, *args, **kwargs):
            from yourapp.forms import BreathingActivityRecordForm
            return BreathingActivityRecordForm
    

    This way, the import will only be evaulated when you actually call .get_form(), and there shouldn’t be any circular dependencies at module loading tme.

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

Sidebar

Related Questions

I have a class Book : from django.db import models from users.models import User
The setup = I have this class, Transcript: class Transcript(models.Model): body = models.TextField('Body') doPagination
I have a Model as follows: class TankJournal(models.Model): user = models.ForeignKey(User) tank = models.ForeignKey(TankProfile)
I have a this model: class Fleet(models.Model): company = models.ForeignKey(Company, editable=False) aircraft = models.ForeignKey(Aircraft)
I have an application fileman with models.py like so: from django.db import models from
I have a view which queries a model to populate a form: class AddServerForm(forms.Form):
I have two models class Friend(models.Model): me = models.ForeignKey(User) friend = models.ForeignKey(User) remark =
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
I have a very simple rails 3 program with 2 models: a user model
I have a view model with a from that includes a set of checkboxes.

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.