I am using this code in my admin.py
from django.db.models import get_models, get_app
for model in get_models(get_app('myapp')):
admin.site.register(model)
But i get warning that get_models is deprecated
How can i do that in django 1.8
This should work,
from django.apps import apps
apps.get_models()
The get_models method returns a list of all installed models. You can also pass three keyword arguments include_auto_created, include_deferred and include_swapped.
If you want to get the models for a specific app, you can do something like this.
from django.apps import apps
myapp = apps.get_app_config('myapp')
myapp.models
This will return an OrderedDict instance of the models for that app.
Related
I created a models within my page but when I attempted to run the page I received an error response
celery_beat_1 | class ClassManager(models.Manager):
celery_beat_1 | NameError: name 'models' is not defined
I searched for fixes to this error online and most of the responses said to implement the
import from django.db import models
function. However, this is something I already have configured in my models page. Any idea on how to proceed forward?
You should import models from django in models.py.
from django.db import models
class MyModel(models.Model):
pass
You can check more information in django documentation itself:
https://docs.djangoproject.com/en/3.2/topics/db/models/#quick-example
You are importing the models in wrong way format, so you have to use -
from django.db import models
rather than using
import from django.db import models
I want to make my models appear in admin site only when debug variable is set to True, when debug is set to False I only want to display one model
I am looking for an elegant way to implement this
I thought I could do something like this:
if not DEBUG:
admin.site._registry = {}
admin.site.register(Model 1)
But where does this code should live? I want it to execute after execution of all admin.py modules from all applications where models registration takes place.
To sum it up
DEBUG = TRUE
Admin site shows:
Model 1
Model 2
Model 3
DEBUG = FALSE
Admin site shows:
Model 1
You can override one of the ready methods of an AppConfig of any app. For example if you have an app named app_name, we can implement an AppConfig that looks like:
# app_name/apps.py
from django.apps import AppConfig
class AppNameConfig(AppConfig):
name = 'app_name'
def ready(self):
from app_name.models import SomeModel
from django.conf import settings
if not settings.DEBUG:
from django.contrib import admin
for cls in list(admin.site._registry):
if cls is not SomeName:
admin.site.unregister(cls)
return super().ready()
with the SomeModel the model you wish to retain.
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
I've made a django app, which is designed to be easily pluggable, and only has 1 view, and 1 model that project planning to use the app need to be aware of.
For ease, I'd like to just make the view and model available from the app-level. So rather than:
from mything.views import MyView
from mything.models import MyModel
You can instead just do:
from mything import MyView, MyModel
I changed the __init__.py file in the app to be like this:
from .views import MyView
from .models import MyModel
Of course I get the old django.core.exceptions.AppRegistryNotReady raised, since it's attempting to run the models.py code before the apps are loaded.
So I came up with the following workaround, and I'm wondering if it's a reasonable pattern to use or not. Now in __init__.py I have:
def _expose_items():
from .views import MyView
from .models import MyModel
globals()['MyView'] = MyView
globals()['MyModel'] = MyModel
And in my app's apps.py:
from . import _expose_items
class MyThingConfig(AppConfig):
name = 'mything'
def ready(self):
_expose_items()
So now indeed, I can directly import the view and model from the outside. Is this useful, or horrible?
Most Django apps do not collect views or models to the top level module. Refer to django.contrib.auth as an example.
Most Django apps do not collect views or models to the top level module. Use clear documentation to demonstrate how to import from your app. Hacks including globals() will probably create more trouble than help.
Refer to django.contrib.auth as an example.
This is my code in my signals.py
from .models import Entry
#receiver(pre_save, sender=Entry)
def do_stuff(sender, instance, *args, **kwargs):
pass
Now this questions is related
Django 1.9 deprecation warnings app_label
But I am not able to figure out why I need to create extra class for that.
Warning:
Model class app.models.Entry doesn't declare an explicit app_label and either isn't in
an application in INSTALLED_APPS or else was imported before its application was loaded.
This will no longer be supported in Django 1.9.
If I just empty my signals file then there is no warning.
The issue is using .models in signals as mentioned in that question
This is mostly likely because your application is not in the INSTALLED_APPS within your settings.py
I also got this error, what I found the error is in model import before it exist.
I used this to import model and it works for me
from django.apps import apps
model_obj = apps.get_model('app_name', 'model_name')
model_obj.objects.get() ...etc