英文:
Problems with Django structuring when entering models, views and serializers in folders?
问题
Here is the translated content you requested:
你好,我正在学习使用Django进行编程,我有一个包含22个表的数据库。正如你所看到的,将所有模型放在一个单独的类中将会非常繁琐和混乱,难以找到或进行修改,序列化器和视图也会有相同的问题。
创建单独的模块是什么样的正确做法,是将其添加到基础项目中,还是创建backend/api/models... backend/api/views... backend/api/serializers/init.py?
我创建了一些基本的序列化器和视图,但是当我将它们与urls.py连接在一起时,出现了编译问题。
有人告诉我,在每个文件夹中创建__init__.py,并在其中集成在该文件夹中创建的内容,但当我尝试将其扩展到其他模块时,会生成错误。
示例:
Models文件夹
Models/__init__.py
from . import Service
Serializers文件夹
Serializers/__init__.py
from . import ServiceSerializer
默认的模型、序列化器和视图文件默认为空白的,这是问题吗?
Views/__init__.py
from . import service_view
Views/service_view.py
from models.Service import Service
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.decorators import api_view
from serializers.service_serializer import ServiceSerializer
# Listar servicios
class ServiceListApiView(generics.ListCreateAPIView):
queryset = Service.objects.all()
serializer_class = ServiceSerializer
service_list_create_view = ServiceListApiView.as_view()
我希望有人可以指导我如何更轻松地导入项目的各个部分,或者告诉我在处理22个或更多表和视图时该如何操作。
英文:
Hello I am learning to program in Django and I have a database of 22 tables, as you can see making all the models in a single class would be very cumbersome and messy to find or make corrections, the same thing happens to me with serializers and views.
How would it be the right thing to create as separate modules to the base project or to make backend/api/models ... backend/api/views ... backend/api/serializers/init.py?
I created something basic serializer and view but when I join it to the urls.py I get compilation problems.
I was told that inside each folder create the init.py and integrate there the from of what is created in that folder, but when I start to spread it to the other modules it generates error.
Example:
Folder Models
Models/__init__.py
from . import Service
Folder Serializers
Serializers/__init__.py
from . import ServiceSerializer
The default models, serializers and views files that are created by default are blank, is that the problem?
Views/__init__.py
from . import service_view
Views/service_view.py
from models.Service import Service
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.decorators import api_view
from serializers.service_serializer import ServiceSerializer
# Listar servicios
class ServiceListApiView(generics.ListCreateAPIView):
queryset = Service.objects.all()
serializer_class = ServiceSerializer
service_list_create_view = ServiceListApiView.as_view()
I hope someone can guide me on how to import parts of the project more easily or tell me how to do it when you work with 22 or more tables and views.
答案1
得分: 0
我建议在您的Django项目中创建多个应用程序。在每个应用程序中,放置与该应用程序相关的模型,视图和序列化程序。
我使用的结构如下:
.
├── accounts
│ ├── __init__.py
│ ├── admin.py
│ ├── api
│ │ ├── __init__.py
│ │ ├── routers.py
│ │ ├── serializers.py
│ │ └── views.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ └── tests
├── config
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── products
│ ├── __init__.py
│ ├── admin.py
│ ├── api
│ │ ├── __init__.py
│ │ ├── routers.py
│ │ ├── serializers.py
│ │ └── views.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ └── tests
在这里,您可以看到我有两个应用程序accounts
和products
。在每个应用程序中,都有一个api
包,其中包含了用于该应用程序的序列化程序、视图和创建相关端点的路由器。
这种方法有助于保持项目更加清晰,使调试更容易,还使项目适合在云中作为微服务进行扩展。
英文:
I would suggest creating multiple apps within your Django project. In each app, place the models, views, and serializers relevant to that app.
The structure I use is the following:
.
├── accounts
│ ├── __init__.py
│ ├── admin.py
│ ├── api
│ │ ├── __init__.py
│ │ ├── routers.py
│ │ ├── serializers.py
│ │ └── views.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ └── tests
├── config
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── products
│ ├── __init__.py
│ ├── admin.py
│ ├── api
│ │ ├── __init__.py
│ │ ├── routers.py
│ │ ├── serializers.py
│ │ └── views.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ └── tests
Here you can see that I have two apps accounts
and products
. In each of these apps, it's placed the api
package, which holds the serializer, the views, and the routers that create the relevant endpoints for the app.
This approach helps to keep a cleaner project and makes debugging easier, it also makes the project suitable for scaling as microservices in the cloud.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论