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.
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)
I am using oscarcommerce for my django project. I want to extend the "StockRecord" model to include some more fields. So I forked the partner app as follows. (boscar is my app name)
python manage.py oscar_fork_app partner boscar/
It successfully forked and new files were added to boscar/partner folder. I added 'boscar.partner' in my installed apps.
Now I added new fields in StockRecord models as follows
boscar/partner/models.py
from django.db import models
from oscar.apps.partner.abstract_models import AbstractStockRecord
class StockRecord(AbstractStockRecord):
effective_price = models.FloatField(default=0, null=True)
is_instock_item = models.BooleanField(default=False, null=True)
instock_quantity = models.IntegerField()
from oscar.apps.partner.models import * # noqa
Now when I try to make migrations it shows the following error.
RuntimeError: Conflicting 'stockrecord' models in application 'partner': <class 'oscar.apps.partner.models.StockRecord'> and <class 'boscar.partner.models.StockRecord'>.
I already successfully forked the catalogue and order models and that are working fine. Only this "StockRecord" models showing this error.
That error can occur as a result of a circular import issue associated with Oscar's support for overriding models and classes.
You need to check for places where you're importing directly from oscar.apps.partner.models. These should be replaced by importing from boscar.partner.models or using oscar.core.loading.get_model.
I've got a weird problem.
I am building a Flask app with SQLAlchemy. I have a file with models, namely, models.py. And I have a User model there.
If I open my "views.py" and insert a string
import models
and then use the User model like
u=models.User.query.filter_by(name='John',password='Doe').first()
everything works fine.
But if instead of "import models" i put
from models import User
Python crashes and says:
ImportError: cannot import name User
how can this be possible?
you most likely have a circular import; your, lets say 'app' module:
# app.py
import models
...
def doSomething():
models.User....
but your models module also imports app
import app
class User:
...
since models imports app, and app imports models, python has not finished importing models at the point app tries to import models.User; the User class has not been defined (yet). Either break the cyclic import (make sure models doesn't import anything that also imports models), or you'll just have to make do with models.User instead of the shorter User in app.
Instead of
from models import User
use
from models import *
In this case, you are importing the models into views.py therefore if you need a class from models, import it from views.py and the circular import problem will be resolved.
I wish to use django import export.
https://pypi.python.org/pypi/django-import-export
While reading the documentation and after installing and configuring the package, I reached the "Creating Import-Export Resource' paragraph:
https://django-import-export.readthedocs.org/en/latest/getting_started.html#creating-import-export-resource
In which directory/file should I insert and save those lines of python that will create the resource?
You can create this file anywhere in your project and then just import it where it's necessary.
So, following the docs, when your app is called core:
#myproject/core/myresources.py
from import_export import resources
from core.models import Book
class BookResource(resources.ModelResource):
class Meta:
model = Book
And then anywhere in your project's code:
from core.myresources import BookResource
dataset = BookResource().export()