models.py
class Tipo(models.Model):
code= models.TextField()
def thename(self):
from myhelp import Diz_Tipi
return Diz_tipi[self.code]
myhelp.py
def creatediz():
return dict_Tipi
Diz_Tipi=creatediz()
This works but I don't think from myhelp import Diz_Tipi should be in that place.
If I add at the start of models.py it return:
File
"/local/lib/python2.7/site-packages/django/apps/registry.py",
line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
there is any other way to make Diz_Tipi available from all the modules?
This sounds like a circular dependency issue.
Note that the error says "Models aren't loaded yet.".
So, are you trying to import one of your models in myhelp? If so, that would create a circular dependency problem, because models.py is importing from myhelp.py and myhelp.py is importing from models.py.
there is a strange issue in django I faced with this, I would add the following to the top of the file:
import django
django.setup()
this in turn should load your apps before attempting model access, also make sure that the app that contains the models file is in the installed apps file in settings
Related
I'm working on a Django based project right now. I 'm getting an error something called AppRegistryNotReady when I'm trying to import a model into another app's model with django get_model() method.. Now the interesting this is, I can import the models from another app in the view files with the same get_model() method.
In views file:
from django.apps import apps
Course = apps.get_model('course', 'Course')
Order = apps.get_model('course', 'Order')
*Now everything is working parfectly.
In models file:
from django.apps import apps
Course = apps.get_model('course', 'Course')
Order = apps.get_model('course', 'Order')
*Now it is getting the following error:
File "/home/mohul/.local/share/virtualenvs/django-backend-and-view-1OsDTUBe/lib/python3.9/site-packages/django/apps/registry.py", line 141, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
This is from Django Docs.
You must define or import all models in your application’s models.py or models/__init__.py. Otherwise, the application registry may not be fully populated at this point, which could cause the ORM to malfunction.
Once this stage completes, APIs that operate on models such as get_model() become usable.
https://docs.djangoproject.com/en/3.2/ref/applications/#how-applications-are-loaded
Finally I got the solution from my own. Many peoples get into this problem I saw around me.
Here how I solved the problem:
project-name/
...project-name/
...apps1/
.....models.py
...apps2/
.....models.py
...manage.py
Just the basic django project structure.
Now to import the models of apps1 into apps2:
In apps2/models.py:
from apps1 import models as apps1Model
# Now accessing the models
apps1Model.Model1
apps1Model.Model2
When I'm trying to migrate a new app onto the server i get this error
AttributeError: module 'django.db.models' has no attribute 'Models'- in terminal
I'm using PyCharm. I am very fresh to Django and web development so any tips will help. Thanks!
from django.db import models
# Create your models here.
class product(models.Model):
item = models.Textfiels()
description = models.Textfields()
price = models.Textfields()
There's no such class django.db.models.TextFields but this works for me on any recent version :
from django.db import models
class product(models.Model):
item = models.TextFiel()
description = models.TextField()
price = models.TextField()
You made 2 typos : the correct name is TextField and you typed Textfields (Python is case sensitive)
I suspect you didn't setup correctly your project under PyCharm. When correctly setup, it shows warnings on misspelled names (names underlined with red dots with default setting).
There's another variation to this question and that is in the form of:
AttributeError: module 'django.contrib.auth' has no attribute 'models'
As far as I can tell this is typically caused by conflicting imports or improperly imported files. Another cause could be changes to Django's updates but I'm not sure about that as I didn't find any documentation that changed that aspect of the Django library.
Short term solution to this is as follows:
from django.contrib.auth import models
class MyClass(models.User): """ """
This will allow you to at least test your runserver command and website on a browser of your choosing.
I'm still trying to figure out any other solutions to this problem that can be a fix for individually importing the 'auth' module itself.
At the time of this writing I'm using Django 2.2.6 whereas Django 2.2.7 is out and 2.2.8 is on the way to be released.
I'm not sure if this is the solution , but when I had this problem it was because in my admin.py file I had
from django.contrib import admin
from meetings.models import Meeting, Room
admin.site.register(Meeting, Room)
But changing it to solved the issue
from django.contrib import admin
# Register your models here.
from meetings.models import Meeting, Room
admin.site.register(Meeting)
admin.site.register(Room)
On circular import of Django Is their any way i can grab a model object with myModel = apps.get_model('app_name', 'model_name') inside models.py file ?
I know i can use models.ForeignKey('app.model',....)
But in my case i am making a query in the models.py for custom function. So that i need to grab the model object. Also can't import it in normal way as already imported this file class in the other file. So must be a circular import.
This code myModel = apps.get_model('app_name', 'model_name') works fine on views.py but in models.py doesn't. Since according to django the all models.py get called after settings.py and after that views and others. so while trying to use get_model inside models.py getting this error
File "/home/mypc/.virtualenvs/VSkillza/lib/python3.6/site-packages/django/apps/registry.py", line 132, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Thanks in advance :)
You can break the circular import by moving the import inside the custom function. That way, the model is loaded when the function runs, not when the module is loaded.
def my_function():
from myapp.models import MyModel
The circular import suggests that your code is structured incorrectly, but we can't give you any suggestions since you haven't shown it.
You can try this.
import importlib
mymodels = importlib.import_module("app.models")
mymodels.YourModel
#query
mymodels.YourModel.objects.all()
I have a bunch of apps, that may or may not contains a file called activity.py. This file basically registers model signals. It works well when I import this file in the ready method of the AppConfig class. The problem is I have a dozen app, so I don't want to have this same method in all my apps :
def ready(self):
# register signal for activity feed
from . import activity
I would like to run a script that will through the INSTALLED_APPS array, and if this app contains a file activity.py, import it.
I can't find a way to run a function for when all the apps are ready, and before the server is listening.
One thing you can do is create another app whose only purpose will be to perform that initialization and put it in INSTALLED_APPS. In that app, subclass AppConfig and override the AppConfig.ready() method.
You can try to use the following approach:
from django.conf import settings
from importlib import import_module
for app in settings.INSTALLED_APPS:
module_name = '%s.%s' % (app, "activity")
try:
import_module(module_name)
except ImportError:
pass
I use mongoengine with django. I have two applications with models.
app1/models.py:
from mongoengine import fields
from mongoengine.document import Document
class Model1(Document):
name = fields.StringField()
lists = fields.ListField(fields.ReferenceField("Model2", dbref=False))
app2/models.py:
from mongoengine import fields
from mongoengine.document import Document
class Model2(Document):
name = fields.StringField()
All applications were added to INSTALLED_APPS. When I use the django dev-server, everything is fine. But using this code with uwsgi-server there is an error:
Model2 has not been registered in the document registry.
Importing the document class automatically registers it, has it
been imported?
What I should do?
You should import app2.models somewhere. Put a comment by the import saying why it's there, so nobody removes the useless-looking import in the future.
When the django dev server starts up it imports the models from all installed apps and validates them. You'll see
Validating models...
0 errors found
This does not happen in a production environment. It is just a nicety of the dev server.