django - detect which apps are user-authored and not packages - django

In my project, I try to enforce logging format for apps that are not imported as packages but created within project by my fellow devs (as logs from these apps can contain user-sensitive data).
For this, I created a logging.Filter that checks if the LogRecord has the name of the module and then conditionally modifies it.
For example, if this is my app list (part of it):
INSTALLED_APPS = [
# django-supplied
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
# third-party apps
'webpack_loader',
'django_tables2',
'django_s3_storage',
# user-created apps
'nlp',
'catalog',
'broker',
]
then the filter checks if the LogRecord.name starts with any of ['nlp', 'catalog', 'broker'].
The question is, how do I detect user-created apps without hardcoding their names into the filter?

The most reliable way would be to hardcode it. But you can do that in your django settings file which would be explicit and maintainable.
# settings.py
CORE_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
]
THIRD_PARTY_APPS = [
'webpack_loader',
'django_tables2',
'django_s3_storage',
]
USER_CREATED_APPS = [
'nlp',
'catalog',
'broker',
]
INSTALLED_APPS = CORE_APPS + THIRD_PARTY_APPS + USER_CREATED_APPS
# customfilter.py
from django.conf import settings
if any(LogRecord.name.startswith(user_app_name)
for user_app_name in settings.USER_CREATED_APPS):
...

Related

AUTH_USER_MODEL reference in settings.py Django

I have a installed an app called 'Login', who have a folder 'models' inside, with a custom_user model. The problem occurs when I tried to configure settings.py, specially auth_user_model.
in installed apps I have the following:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'project_app.login'
]
and below
AUTH_USER_MODEL = 'login.models.CustomUser'
But I have the following error: "Invalid model reference. String model references must be of the form 'app_label.ModelName'." I put the .models in AUTH_USER_MODEL, because I want to reference the app that the CustomUser is inside the folder "models" in Login.
Also, I tried with declare like this:
AUTH_USER_MODEL = 'login.CustomUser'
but the error is this: 'AUTH_USER_MODEL refers to model 'login.CustomUser' that has not been installed'
The issue is with the way that your app is installed - based on your AUTH_USER_MODEL, login should be the name of the app. It is acceptable to contain Django apps within folders for organisation purposes - however, your parent folder is project_app which from the name also appears to be an app. It's hard to say for certain what the issue is without knowing your project structure but I would expect changing your installed apps to just project_app and your AUTH_USER_MODEL to project_app.CustomerUser should work.
If your app name is "login", you can proceed as follows:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
#apps
'login.apps.LoginConfig']
I think the model in your login application is as follows:
class CustomUser(AbstractBaseUser):
#some fields for customuser
and in settings.py:
AUTH_USER_MODEL = 'user.CustomUser'
AUTH_USER_MODEL = 'app_name.ModelName'

One Django app in a separate python package

Django==3.2.5
Have a look at this picture:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'clients',
'general',
'images',
'themes',
'omnibus',
'staticassets',
'categories',
'tags',
'articles',
]
When I run this, I get:
ModuleNotFoundError: No module named 'articles'
It is predictable as Django doesn't know where to find the app.
Then I tried like this:
INSTALLED_APPS = [
'dependencies.articles',
]
No success.
Here 'articles' is just an ordinary Django app like categories or clients. But I'd like to move it to 'depencencies' package. Why? I'd like to organize the code structure like this for as personal preferences. How can I cope with this problem, assuming it is technically possible?
cut the articles directory back to the main block project directory
as of now :
blog_project
|
|---dependencies
|
----- articles
needs to be :
blog_project
|
|---articles
get rid of the dependencie directory it no use

improperly configured app in settings.py of django

Tying to register my djano app in the settings section of my django project. but when i run the server i am getting an error in the terminal.
django.core.exceptions.ImproperlyConfigured: 'site1app.apps' does not contain a class 'Site1appConfig'. Choices are: 'Site1AppConfig'.
I dont know what's going wrong as i just added it to the list of installed apps in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'site1app.apps.Site1appConfig',
]
This looks like a miss on the capitalization for your class
Change:
'site1app.apps.Site1appConfig'
To:
'site1app.apps.Site1AppConfig'

What is the difference between Django syntax

hello guys what the difference between this two syntax
'blog' and 'blog.apps.BlogConfig'
in project this is located in installed app.
I think both are same but there should be difference between this 2 commands because they looks like different
first one is this
INSTALLED_APPS = [
'blog',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
seccond one is :
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
I assume that you have a custom configuration(BlogConfig) for your app.
You have at least two options to register the app with custom config in project settings(INSTALLED_APPS).
(if)You have a variable default_app_config = 'blog.apps.BlogConfig' in blog/__init__.py file, then inserting blog into INSTALLED_APPS will be the same as blog.apps.BlogConfig. If you don't have it in __init__.py, BlogConfig won't be applied in the example below.
INSTALLED_APPS = [
...,
'blog',
...,
]
(if)You have an empty __init__.py in your app. Then to apply the custom config you must include the path to config.
INSTALLED_APPS = [
...,
'blog.apps.BlogConfig',
...,
]
putting only blog in the second example will work without the custom config.

django-environ: how to separate INSTALLED_APPS for dev and prod

I am using djano-environ for my settings
In django development i am planning to install django-extensions app which i need only in development. So what is the recommended way to use django-environ for prod and dev w.r.t INSTALLED_APPS
I have to add it to the INSTALLED_APPS.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# third party apps
'django_extensions',
]
Now how to use django-envion's .env file which will pass INSTALLED_APPS
because i will have a different .env files for dev and prod
I have separate dev.py and prod.py since I've config keys which are not applicable in both the environments.
If you've multiple cases like this a good option would be to create a dev.py
dev.py
from base import *
INSTALLED_APPS += ['django_extensions', ]
Else if this is a one off case, you can load additional app list from your env file.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
] + os.list('ENV_SPECIFIC_APPS')
envfile
.
.
ENV_SPECIFIC_APPS=django_extensions