我喜欢把我的django应用程序拆分到多个文件夹中,就像图中一样。
示例
Bank
---views
---urls
---models
---serializer
Currency
---views
---urls
---models
---serializer
我把__init.py__
文件放在每个文件夹里,这里是银行文件夹里的init文件`
from .models import BimaCoreBank`
这里是models.py
for bank文件夹
from core.abstract.models import AbstractModel
from core.country.models import BimaCoreCountry
from core.state.models import BimaCoreState
class BimaCoreBank(AbstractModel):
name = models.CharField(max_length=128, blank=False, unique=True)
street = models.CharField(max_length=256, blank=True, null=True)
street2 = models.CharField(max_length=256, blank=True, null=True)
zip = models.CharField(max_length=16, blank=True, null=True)
city = models.CharField(max_length=16, blank=True, null=True)
state = models.ForeignKey(
BimaCoreState, on_delete=models.PROTECT)
country = models.ForeignKey(
BimaCoreCountry, on_delete=models.PROTECT)
email = models.EmailField(blank=True, null=True)
active = models.BooleanField(default=True)
bic = models.CharField(max_length=16, blank=True, null=True)
def __str__(self) -> str:
return self.name
class Meta:
ordering = ['name']
permissions = []
app_label = "BimaCoreBank"
这里是银行文件夹中的urls.py
文件
从rest_framework导入路由器
从core.bank.views导入BimaCoreBankViewSet
router = routers.DefaultRouter()
app_name = 'BimaCoreBank'
urlpatterns = [
router.register(r'bank', BimaCoreBankViewSet, basename='bank'),
]
每个文件夹都是一样的,只是我改了类名
现在,在core文件夹(包含所有子文件夹的app文件夹)中的__init.py__
文件中
from . import bank
from . import country
from . import currency
from . import state
int的urls.py
在核心文件夹我有这个
from django.urls import path, include
app_name = 'core'
urlpatterns = [
path('currency/', include('currency.urls.py')),
path('country/', include('country.urls.py')),
path('state/', include('state.urls.py')),
path('bank/', include('bank.urls.py'))
]
最后在app文件夹(我的项目的主文件夹)中,我在www.example.com中有这个urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('api/schema/', SpectacularAPIView.as_view(), name='api-schema'),
path(
'api/docs/',
SpectacularSwaggerView.as_view(url_name='api-schema'),
name='api-docs',
),
path('api/user/', include('user.urls')),
path('api/core/', include('core.urls'))
]
但没有成功,
第一次我添加app_label
在meta class
foreach文件夹(银行,货币...)
然后我在urls.py
文件中添加了'app_name'
,它也不起作用
现在的错误消息是这样的
(venv) rdm-bima@rdmbima-HP-EliteBook-850-G8-Notebook-PC:~/Downloads/bima_chiffre/app$ python3 manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
autoreload.raise_last_exception()
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 398, in execute
autoreload.check_errors(django.setup)()
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/apps/config.py", line 193, in create
import_module(entry)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/rdm-bima/Downloads/bima_chiffre/app/core/__init__.py", line 1, in <module>
from . import bank
File "/home/rdm-bima/Downloads/bima_chiffre/app/core/bank/__init__.py", line 1, in <module>
from .models import BimaCoreBank
File "/home/rdm-bima/Downloads/bima_chiffre/app/core/bank/models.py", line 4, in <module>
from core.abstract.models import AbstractModel
File "/home/rdm-bima/Downloads/bima_chiffre/app/core/abstract/models.py", line 7, in <module>
from auditlog.registry import auditlog
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/auditlog/registry.py", line 325, in <module>
auditlog = AuditlogModelRegistry()
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/auditlog/registry.py", line 52, in __init__
from auditlog.receivers import log_access, log_create, log_delete, log_update
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/auditlog/receivers.py", line 6, in <module>
from auditlog.context import threadlocal
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/auditlog/context.py", line 9, in <module>
from auditlog.models import LogEntry
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/auditlog/models.py", line 10, in <module>
from django.contrib.contenttypes.fields import GenericRelation
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/contrib/contenttypes/fields.py", line 7, in <module>
from django.contrib.contenttypes.models import ContentType
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/contrib/contenttypes/models.py", line 137, in <module>
class ContentType(models.Model):
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/db/models/base.py", line 127, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
self.check_apps_ready()
File "/home/rdm-bima/Downloads/bima_chiffre/venv/lib/python3.10/site-packages/django/apps/registry.py", line 138, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
我的问题是,是否可以像我一样将一个django应用程序拆分到多个文件夹中?如果不能,如何拆分一个django应用程序,因为我不喜欢在同一个文件中有多个模型类
谢谢你
对于settings file
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'auditlog',
'drf_spectacular',
'core',
'user',
]
1条答案
按热度按时间juzqafwq1#
是的,你可以任意分割你的django应用程序,write up介绍了这一点。
您可以检查django command在您的应用程序中是否检测到任何已知错误:
或
如果出现此错误,
django.core.exceptions.AppRegistryNotReady:尚未加载应用。
然后,没有看到您的设置文件;检查您是否已在INSTALLED_APPS中添加了应用程序,还检查您是否添加了SECRET_KEY。