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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:12:21+00:00 2026-05-16T02:12:21+00:00

I have some django apps which are versioned by app name. appv1 appv2 The

  • 0

I have some django apps which are versioned by app name.

appv1
appv2

The models.py in the apps are slightly different based on the version, but have the same model names.

I’m attempting to load the models dynamically into the current namespace.
So I’ve made a function that attempts to get the module and return it:

def get_models_module(release):
    release_models_path = u"project.data_%s" % (release)
    _temp = __import__(release_models_path, globals(), locals(), ["models"], -1)
    models = _temp.models
    return models

Then I try to load all the models from the returned models module but it fails.

models = get_models_module("1")
from models import *

When this happens I get the error:

ImportError: No module named models

I checked and the returned “models” object is listed as “module ‘project.data_1.models’ …”,
but apparently it doesn’t like to be renamed.

Is there a way I can load all the defined models from the specific app revision?
Or, is there a better way to handle this kind of situation?

Note: This is currently only in the load function to get data into the database, and isn’t run through any views.


Updated Solution:

Thanks Daniel Kluev for the solution, here’s my updated function:

def load_release_models(release):
    model_release = release.replace(u".", u"_").replace(u"-", u"d") 
    release_models_path = u"project.data_%s.models" % (model_release)
    # import all release models into (global) namespace
    exec(u"from {0} import *".format(release_models_path)) in globals()

Note – I’m loading into globals since I need access to these models within the whole file.

  • 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-16T02:12:21+00:00Added an answer on May 16, 2026 at 2:12 am

    At from models import * you are NOT referring to the models variable. You are just trying to import module called ‘models’, which, obviously, does not exist.

    You can use hack like this to import everything from the module into current namespace:

    ldict = locals()
    for k in models.__dict__:
        if not k.startswith('__') or not k.endswith('__'):
            ldict[k] = models.__dict__[k]
    

    Or use exec() to load the module,

    exec("from project.data_{0}.models import *".format(release)) in locals()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So in my Django project I have a few different apps, each with their
Short version: I have a Django project under development & testing (not yet into
I have a workflow(on svn) which I am trying to replicate with git, but
I kept thinking a lot about the meaning of migrating a Django app the
I have built my website in Django. And like any other django project I
Short version: Is there a simple, built-in way to identify the calling view in
The Django documentation states the following clearly: Not every template in contrib\admin\templates\admin may be
I am a current web2py user, but find I still go back to Django
I built an inventory database where ISBN numbers are the primary keys for the

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.