refactor: 重构配置文件

所有项目的app都放在env.py里的CUSTOM_APPS列表里注册,为了支持按照项目app查找model
This commit is contained in:
ahhui
2023-08-03 18:04:34 +08:00
parent 06d7d718e1
commit b2aa22ce84
4 changed files with 40 additions and 5 deletions

2
backend/.gitignore vendored
View File

@@ -91,7 +91,7 @@ ENV/
**/migrations/*.py **/migrations/*.py
!**/migrations/__init__.py !**/migrations/__init__.py
*.pyc *.pyc
conf/ conf/*
!conf/env.example.py !conf/env.example.py
db.sqlite3 db.sqlite3
media/ media/

View File

@@ -55,10 +55,10 @@ INSTALLED_APPS = [
"rest_framework", "rest_framework",
"django_filters", "django_filters",
"corsheaders", # 注册跨域app "corsheaders", # 注册跨域app
"dvadmin.system",
"drf_yasg", "drf_yasg",
"captcha", "captcha",
'channels', 'channels',
*locals().get("CUSTOM_APPS", []), # 所有项目里写的app需要在env.py文件里的CUSTOM_APPS中
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@@ -306,7 +306,7 @@ AUTHENTICATION_BACKENDS = ["dvadmin.utils.backends.CustomBackend"]
# ================================================= # # ================================================= #
SIMPLE_JWT = { SIMPLE_JWT = {
# token有效时长 # token有效时长
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=120), "ACCESS_TOKEN_LIFETIME": timedelta(minutes=1440),
# token刷新后的有效时间 # token刷新后的有效时间
"REFRESH_TOKEN_LIFETIME": timedelta(days=1), "REFRESH_TOKEN_LIFETIME": timedelta(days=1),
# 设置前缀 # 设置前缀

View File

@@ -0,0 +1,10 @@
DATABASE_ENGINE = 'django.db.backends.mysql'
DATABASE_NAME = 'dvadmin3'
DATABASE_USER = 'root'
DATABASE_PASSWORD = 'root'
DATABASE_HOST = '127.0.0.1'
DATABASE_PORT = '3306'
ALLOWED_HOSTS = ['*']
CUSTOM_APPS = [
"dvadmin.system",
]

View File

@@ -6,15 +6,17 @@
@Created on: 2021/5/31 031 22:08 @Created on: 2021/5/31 031 22:08
@Remark: 公共基础model类 @Remark: 公共基础model类
""" """
from importlib import import_module
from django.apps import apps from django.apps import apps
from django.db import models from django.db import models
from django.db.models import QuerySet from django.conf import settings
from application import settings from application import settings
table_prefix = settings.TABLE_PREFIX # 数据库表名前缀 table_prefix = settings.TABLE_PREFIX # 数据库表名前缀
class SoftDeleteQuerySet(QuerySet): class SoftDeleteQuerySet(models.QuerySet):
pass pass
@@ -106,3 +108,26 @@ def get_all_models_objects(model_name=None):
if model_name: if model_name:
return settings.ALL_MODELS_OBJECTS[model_name] or {} return settings.ALL_MODELS_OBJECTS[model_name] or {}
return settings.ALL_MODELS_OBJECTS or {} return settings.ALL_MODELS_OBJECTS or {}
def get_all_custom_app_models():
"""获取所有项目写的app里的models"""
for app in settings.CUSTOM_APPS:
model_module = import_module(app + '.models')
filter_model = [
getattr(model_module, item) for item in dir(model_module)
if item != 'CoreModel' and issubclass(getattr(model_module, item).__class__, models.base.ModelBase)
]
model_list = []
for model in filter_model:
fields = [
{'title': field.verbose_name, 'name': field.name, 'object': field}
for field in model._meta.fields
]
model_list.append({
'verbose': model._meta.verbose_name,
'model': model.__name__,
'object': model,
'fields': fields
})
yield model_list