Django setting - apps aren't loaded yet - django

I want to import an apps model in setting.py for PUSH_NOTIFICATIONS_SETTINGS in django-push-notifications. This is my code:
INSTALLED_APPS = (
....
'my_app',
'push_notifications'
....
)
from my_app.models import User
PUSH_NOTIFICATIONS_SETTINGS = {
'GCM_API_KEY': 'xxxxx',
'APNS_CERTIFICATE': 'xxxxx.pem',
'USER_MODEL': User, # i want to change the default from auth_user to my_app User
}
But it raise an error on this line:
from my_app.models import User
The error is:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
How Can i load my_app model in setting.py?

You cannot load models from inside your settings file like that - models can only be loaded once all apps are loaded (which can only happen after the settings have been loaded).
Looking at the code in django-push-notifications, you should be able to provide the model as a string with dotted path:
'USER_MODEL': 'my_app.User'

You can't load User model in settings, but instead you can change it
PUSH_NOTIFICATIONS_SETTINGS = {
'GCM_API_KEY': 'xxxxx',
'APNS_CERTIFICATE': 'xxxxx.pem',
'USER_MODEL': 'my_app.User',
}
And use it later like:
from django.apps import apps
from django.conf import settings
User = apps.get_model(settings.PUSH_NOTIFICATIONS_SETTINGS['USER_MODEL'])
And you can do whatever you want with this User model

Related

filter all the installed apps with their model and permission in django

I want to display list of all the installed app and model as well as their permission.
for example
app_name_1
model_name_1
permission_1
permission_2
...........
model_name_2
permission_1
permission_2
...........
app_name_2
model_name_1
permission_1
permission_2
...........
model_name_2
permission_1
permission_2
...........
and so on.
You can get the list of installed apps (excluding Django defaults):
from django.apps import apps
from django.conf import settings
from django.contrib.auth.models import Permission
apps_list = [app for app in settings.INSTALLED_APPS if not 'django' in app]
then get all models:
models = []
for app in apps_list:
models.append(apps.all_models[app])
next is actually getting the permissions:
for model in models:
app_lbl = model._meta.app_label
model_name = model._meta.model_name
permissions = Permission.objects.filter(content_type__app_label=app_lbl, content_type__model=model_name)

Django REST API Generics displaying improperly

Currently using a REST API and the generic views CreateUpdateDestroy, and my admin display GUI looks like this :
All the sources online that I've followed, tutorials etc get a generic view which looks much nicer.
Here is my views.py:
from rest_framework import generics
from models import Results
from serializers import ResulutsSerializer
class ResultsList(generics.ListAPIView):
queryset = Results.objects.all()
serializer_class = ResultsSerializer
class ResultsDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Results.objects.all()
serializer_class = ResultsSerializer
and urls.py:
from django.urls import path
from main import views
urlpatterns = [
path('results/', views.ResultsList.as_view()),
path('<int:pk>/', views.ResultsDetails.as_view())
]
what am I doing wrong?
It looks like you need to collect your app assets:
$ python manage.py collectstatic
# You can provide option: --settings=<your-settings-file> if you're using custom settings which is not default in manage.py
You will need to configure staticfiles settings in your Django settings module if not already configured – e.g. settings.py. Please follow documentation at:
https://docs.djangoproject.com/en/2.0/howto/static-files/
https://docs.djangoproject.com/en/2.0/ref/contrib/staticfiles/
If you are developing locally:
You should set DEBUG=True in your Django Settings Module (i.e. normally settings.py)

Django Cookiecutter: How to import AUTH_USER_MODEL in config/settings/base.py?

As we know, Django-Cookiecutter has a different setup for settings files. The regular from django.conf import settings doesn't work here.
I want to reference to the custom user model defined in the base.py file in the settings directory. Any ideas?
Below is my project layout:
repository/
config/
settings/
__init__.py
base.py # where the auth_user_model variable is defined
local.py
production.py
test.py
app_dir/
users/
__init__.py
models.py # where the custom user model is stored
I also tried to import the custom user model directly from users/models.py as below:
from users.models import User
But got the following error:
RuntimeError: Model class users.models.User doesn't declare an
explicit app_label and isn't in an application in INSTALLED_APPS.
Tried the following, and it seems to work so far:
from config.settings.base import AUTH_USER_MODEL
from django.contrib.auth import get_user_model
It'll return django user class that you defined in base.py
Edit:
from django.conf.settings import AUTH_USER_MODEL

enable a model on Django Admin on Dreamhost

Hi I've created a new app (Paginas) but I cannot see inside the admin page. I've added 'paginas' to INSTALLED_APP. My admin.py is
# coding=utf-8
from django.contrib import admin
from .models import Pagina
class PaginaAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("titulo",)}
list_display = ('titulo' , 'contenido')
class Media:
js = ('/layout/grappelli/tinymce/jscripts/tiny_mce/tiny_mce.js','/layout/grappelli/tinymce_setup/tinymce_setup.js')
admin.site.register(Pagina,PaginaAdmin)
I am using passenger_wsgi.py file, I've touched tmp/restart.txt file, I've killed python process
I don't know what else can I do
The project you can see it on github https://github.com/totechess/paralisis_cerebral
What kind of error do you get when you go to your site yoursite.com/admin/
Did you add admin to INSTALLED_APPS? Note the S
....
'django.contrib.admin',
....
Perhaps your urls.py are not updated properly? What is the error?

Django admin: Can't add app to admin

I have installed Django on my host (I use their install version 1.1.1), all is working fine.
I have created some apps and they are registered in my settings.py (I can verify this works when i visit the site, the app shows up).
In the folder of this app i have created admin.py with the following content:
from progmaticnet.page.models import Page, PageContent
from django.contrib import admin
class PageContentInline( admin.StackedInline ):
model = PageContent
extra = 1
max_num = 1
class PageAdmin( admin.ModelAdmin ):
inlines = [ PageContentInline ]
class Media:
#js = ('/site_media/js/tinymce/jscripts/tiny_mce/tiny_mce.js', '/site_media/js/tinymce/textarea.js')
admin.site.register( Page, PageAdmin )
But my app doesn't show up in the admin... It is said in the documentation that you'll need to restart the server, although i can't do that (runs on apache), i have a dispatch.fcgi with this content:
#!/usr/bin/python
import sys, os
project_sys="/home/progmati/public_html"
#add a custom python path
sys.path.insert(0, project_sys)
# set the DJANGO_SETTINGS_MODULE environment variable
os.environ['DJANGO_SETTINGS_MODULE'] = 'progmaticnet.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded")
I have killed the process and started it anew but with no avail...
Does anybody know what to do about it?
Thanks
Why is the js declaration commented out in your Media class? That looks like it'll be an invalid class definition as a result (class definitions can't be entirely empty). Try uncommenting it, or adding pass below the commented out line, like this:
class Media:
#js = ('/site_media/js/tinymce/jscripts/tiny_mce/tiny_mce.js', '/site_media/js/tinymce/textarea.js')
pass