maxtepkeev / architect

A set of tools which enhances ORMs written in Python with more features
Other
391 stars 57 forks source link

And run 'architect --module...' give me Import Error (Django) #57

Open vncsms opened 5 years ago

vncsms commented 5 years ago

I add the partition configuration in my class:

          @architect.install('partition', type='range', subtype='integer', constraint='100', column='id')
          class Article(ArticleGeneric,
               ConfidenceScoreModel,
               EditableModel,
               HitTrackedModel,
               HotnessScoreModel,
               VotableModel):

I export the path: $ export DJANGO_SETTINGS_MODULE=prisvo.settings

But when i execute architect partition --module article.models

Traceback (most recent call last):
  File "/home/developer/.virtualenvs/prisvo/bin/architect", line 11, in <module>
    sys.exit(main())
  File "/home/developer/.virtualenvs/prisvo/local/lib/python2.7/site-packages/architect/commands/__init__.py", line 91, in main
    orms.init()
  File "/home/developer/.virtualenvs/prisvo/local/lib/python2.7/site-packages/architect/orms/__init__.py", line 17, in init
    getattr(__import__(name, globals(), level=1), 'init', lambda: None)()  # if yes, run init() for this ORM
  File "/home/developer/.virtualenvs/prisvo/local/lib/python2.7/site-packages/architect/orms/django/__init__.py", line 7, in init
    django.setup()
  File "/home/developer/.virtualenvs/prisvo/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/developer/.virtualenvs/prisvo/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/developer/.virtualenvs/prisvo/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/developer/projects/prisvo/prisvo-backend/article/models.py", line 17, in <module>
    from edition.base import EditableModel
  File "/home/developer/projects/prisvo/prisvo-backend/edition/base.py", line 6, in <module>
    from user.models import User
ImportError: No module named models

My site works with no problem, my model user work perfectly, my migrations are done without errors. Only here is giving me an error. And if i comment where the import is imported is give another import error with the same model but in another code.

dmitrykaramin commented 5 years ago

Probably you firstly should try to import User model not directly but from get_user_model() from django.contrib.auth

vncsms commented 5 years ago

Ok, i add django.contrib.auth.models import User instead from user.models import User. Now the commands architect partition --module article.models works. But i cannot add users, because several errors. So i revert my import to from user.models import User, and now works fine. I actually don't know what is happening, but i think my problem is solved. Thanks

dmitrykaramin commented 5 years ago

Try to avoid direct User import. Import from django.contrib.auth.models also not a good way.

vncsms commented 5 years ago

But if cannot import with this two ways, how i import my model User?

dmitrykaramin commented 5 years ago
from django.contrib.auth import get_user_model
User = get_user_model()
vncsms commented 5 years ago

The problem is the django.contrib.auth.models import User and from user.models import User aren't the same, i have my own user model. And when i use django.contrib.auth is not giving me my model. I think my error: ImportError: No module named models it's happening because the conflict between another object that calls user. But in my project this conflict not occur, only when i execute the architect.