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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:49:08+00:00 2026-05-28T22:49:08+00:00

I wrote a short snippet to dynamically generate all of my model forms within

  • 0

I wrote a short snippet to dynamically generate all of my model forms within an app. To me, the most logical place to call this snippet is in the app’s forms.py. But this causes a circular import since I’m trying to set attributes in the module that called me, and it doesn’t exist yet. Is there an elegant way around this?

#bomgar.forms.py

import base.forms
import bomgar       # bomgar is myapp

base.forms.generate_base_forms(bomgar)

#base.forms.py

from django.db import models
from django import forms
import inspect


class BaseForm(forms.ModelForm):
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model=None


''' Generates a base set of forms to work off of. So I dont have to
    make a billion of them by hand to start off. '''
def generate_base_forms(module):
    __import__('%s.models' % module.__name__)
    __import__('%s.forms' % module.__name__)    
    for model_name, model_class in inspect.getmembers(module.models):
        if inspect.isclass(model_class):
            if model_class._meta.abstract == False:
                form_name=model_name+'Form'
                class ModelMeta(BaseForm.Meta):
                    model=model_class
                form_class=type(form_name,(BaseForm,), { 'Meta': ModelMeta })
                setattr(module.forms, form_name, form_class)

Terminal Output

(env)Macbook:djinmanage me$ ./manage shell
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import bomgar.models
>>> import bomgar.forms
<module 'bomgar.models' from '/Users/me/Sites/djinmanage/djinmanage/bomgar/models.pyc'>
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/me/Sites/djinmanage/djinmanage/bomgar/forms.py", line 4, in <module>
    base.forms.generate_base_forms(bomgar)
  File "/Users/me/Sites/djinmanage/djinmanage/base/forms.py", line 19, in generate_base_forms
    print module.forms
AttributeError: 'module' object has no attribute 'forms'
>>>
  • 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-28T22:49:09+00:00Added an answer on May 28, 2026 at 10:49 pm

    Call the function to generate the forms at the end of the module, after all the required names have been bound.

    EDIT:

    Okay, you imported them. But you let the imports fall on the floor.

    impmodule = __import__('%s.models' % module.__name__)
    __import__('%s.forms' % module.__name__)    
    
       ...
    
        setattr(impmodule.forms, form_name, form_class)
    

    (You don’t need to catch the result of the second __import__ since they’re both the same module.)

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

Sidebar

Related Questions

To help me better understand lambda I wrote this short snippet that rotates and
So I wrote this short script (correct word?) to download the comic images from
I've wrote an App that streams a short movie over a fix URL. Everything
Just wrote a short script of copying all my tables into another local database.
I wrote a short bash script to complete a task that involves creating a
I wrote a short bit of code to simply skip num_lines lines in an
I wrote a short C++ program to do XOR encryption on a file, which
I call a .Net web service from my Blackberry app. After having nothing but
I have a similar problem.This is a snippet of my source: FileWriter fstream =
I wrote a short Applescript to move the selected messages to the relevant archive

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.