I have such file order:
project/
app/
models.py
signals.py
I am keeping signals inside signals.py as it should be. and at the top of the signals.py file, I include myapp models as I do queries in these signals with
from myproject.myapp.models import Foo
However it doesnt seem to find it, as I run the server or validate from manage.py, it gives this error:
from myproject.myapp.models import Foo
ImportError: cannot import name Foo
I am using Django 1.2.1.
Most likely you have a circular dependency. Does your models.py import the signals? If so, this can't work as both modules now depend on each other. You may need to import the models within a function in the signals file, rather than at the top level.
Related
I created a model in a Django application. Let's say I have a model class Car in models.py. This class has a method called refuel() that interacts with the class' properties.
I want to write a Python script that imports models.py, instanciates the class and calls refuel() so to then debug the class using Breakpoints with my IDE.
However, for some reason, I cannot import the model file in another script. When I use this syntax to import the file:
from .models import Car
I get this error:
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
Note that the script I import the model in is located in the same folder as models.py.
What's the way of solving this problem, or, more broadly, what's the best way of debugging my model file ?
Thanks.
I have a utils.py file in an app directory at the same level as models.py. I am trying to import a class (a model) in utils.py:
from models import TeamConstraint
My IDE does not report any conflict, however when running the server I am getting an ImportError:
ImportError: cannot import name TeamConstraint
I tried checking for circular dependencies, there is a file tasks.py also in the same level as the rest that imports from utils.py:
from scheduler.utils import current_indie_teams, matchcount_by_week
Although I don't understand why here scheduler needs to be included explicitly, while when trying to import a model it is not required (or so says PyCharm at least).
I am trying to figure out how to solve this.
When you say from models import TeamConstraint it's not clear what models is.
It could be a different app with the name models or it could be the file in the same folder. It's better to be explicit than implicit.
Try using an absolute import from scheduler.models import TeamConstraint or if you really want to use a relative import do so as such from .models import TeamConstraint (Notice the . before models)
Also it seems like you are using Python3 but your PyCharm is configured for Python2, else it would warn you.
whenever you get import errors, try delaying one of the imports.
here place 'from models import TeamConstraint' in class or function instead.
I have a Pyramid app, I split models AND views into separate files in following manner:
How do I split models.py into different files for different models in Pyramid?
One small consequence for views is that since I have them in separate view files in a "views" package is that they cannot find models.py, now models package, since it resides in parent directory.
That is, it used to be:
models.py
views.py
Now it's:
views/__init__.py
views/view1.py
views/view2.py
models/__init__.py
models/model1.py
models/model2.py
Therefore, importing from models in a view results in:
from models import (
ImportError: No module named models
Now, I can work around this by adding following path search module extension in views/__init__.py:
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
But that's kind of kludgy. Is there a better, maybe Pyramid-specific solution?
This is not really pyramid specific, it is just python.
See https://docs.python.org/2/tutorial/modules.html#packages
for a detailed explanation.
You want to
import models.model1
Or
from models.model2 import (...)
I'm late, but it can help some other person.
For solve this, you need to specify your project name. In your case, if the project name is projet_name import your models like this.
from project_name.models.model1 import *
or
import project_name.models.model1
I suppose is not the best way, but it work fine for me.
How can I import all models in the settings.py in INSTALLED_APPS? When i`m trying to insert some model there is an error occurred: "no models named app1_model"
-ProjectName
--ProjectName
---models
----__init__.py
----admin.py
----app_1_model
----....
----app_n_model
---templates
---__init__.py
---settings.py
---urls.py
---wsgi.py
--manage.py
^ Structure of project ^
The INSTALLED_APPS is for apps not for models. Models are classes that live within your app, usually in /«app_name»/models.py.
I think you have misunderstood how a Django project is structured. Try working through a tutorial example.
a typical structure:
/«project»/«app_name»/models.py
and in settings:
INSTALLED_APPS = [ ... '«app_name»' ... ]
Your path will contain the base project directory, so you can import your app where you need it.
And to use them:
from «app_name».models import *
Although it is always best not to import *, instead, name the classes you wish to import.
To answer the question in the comment:
If you don't like the idea of storing all your models in one file (even though it is normal to do this), you can create a module called models. To do this, create a directory called /«project»/«app_name»/models, inside it put __init__.py (to declare it as a module) and then create your files inside there. You then need to import your file contents into the module in __init__.py. You should read about Python modules to understand this.
To answer the second comment question:
To view models in admin, you should create an admin file with model admin objects.
And finally:
Please read through the tutorials to ensure you have a thorough understanding of Django. Otherwise you're wasting your own time!
I am having problems importing models between my apps in my current django project. When trying to run the dev server or sync the database, I receive the error:
File "/path/to/Project/../Project/app1/models.py", line 3, in <module>
class SomeModel(Thing):
NameError: name 'Thing' is not defined
Thing is defined in Project/main/models.py which I import into Project/app1/models.py via:
from project.main.models import Thing
I am aware that this sort of thing can be the result of python preventing circular imports.
Currently I have three django apps: main, app1, app2
The imports for each app are like so:
main:
from project.app1.models import AnotherThing
app1:
from project.main.models import Thing
app2:
from project.main.models import Thing
from project.app1 import Something
I suspect that importing models from app1 into app2 is causing the problem, since both of those apps import models from main, which in turn imports models from app1, etc.
If this is the case - what other methods can I use to achieve the same effect as these imports?
Is there a more accepted way of organizing things so that I won't run into this?
If you want to subclass those models, you need to remove circular dependency. Refactor common stuff into another module, and make those two import that instead of each other. If the model is only used as a foreign key, you can use string instead of an object to defer the import (e.g. ForeignKey('app.Model') — see documentation for details).