外观
AI辅助编码(后端)
本文展示一个完整的开发示例:实现图书的增删改查功能(后端)
创建模型
打开豆包、通义或DeepSeek等工具,输入提示词
- 根据实际需求,替换【】中内容
怎样设计【图书管理】数据库表整理输出结果,然后针对其中一张表,创建模型。提示词
- 根据实际需求,替换【】中内容
python
根据下面信息,参考例子,写出Django模型
- 继承类 BaseModel
- 日期类型:DateTimeField
- 枚举类型:SmallIntegerField
- 如果使用到枚举,示例如下
class MenuTypeEnum(Enum):
"""菜单类型枚举"""
DIR = 1 # 目录
MENU = 2 # 菜单
BUTTON = 3 # 按钮
###
【
图书表DemoBook:
id
title 书名
author 作者
publisher 出版社
publish_date 出版日期
category 图书分类:历史、经济、儿童
total_copies 总册数
price 单价(2位小数)
】
###
参考例子:
from django.db import models
from mars_framework.db.base import BaseModel
from mars_framework.db.enums import CommonStatusEnum
class SystemPost(BaseModel):
id = models.BigAutoField(primary_key=True, db_comment="岗位ID", help_text="岗位ID")
code = models.CharField(
max_length=64,
unique=True,
db_comment="岗位编码",
help_text="岗位编码",
error_messages={"unique": "岗位编码已存在"},
)
name = models.CharField(
max_length=50,
unique=True,
db_comment="岗位名称",
help_text="岗位名称",
error_messages={"unique": "岗位名称已存在"},
)
sort = models.IntegerField(
db_comment="显示顺序",
help_text="显示顺序",
)
status = models.SmallIntegerField(
choices=[(item.value, item.name) for item in CommonStatusEnum],
db_comment="部门状态",
help_text=" 部门状态",
)
remark = models.CharField(
max_length=500,
blank=True,
null=True,
db_comment="备注",
help_text="备注",
)
class Meta:
managed = True
db_table = "system_post"
db_table_comment = "岗位信息表"
ordering = ["-id"]编写序列化器
针对上一步得到的Django模型,编写序列化器。提示词:
python
根据下面信息,参考例子,编写模型的两个序列化器
- BaseModel类中定义了create_time字段
- 导出字段:【 id title author publish_date category total_copies 】
###
模型定义:
【
class DemoBook(BaseModel):
id = models.BigAutoField(primary_key=True, db_comment="图书ID", help_text="图书ID")
title = models.CharField(
max_length=255,
db_comment="书名",
help_text="书名",
)
author = models.CharField(
max_length=100,
db_comment="作者",
help_text="作者",
)
publisher = models.CharField(
max_length=100,
db_comment="出版社",
help_text="出版社",
)
publish_date = models.DateTimeField(
db_comment="出版日期",
help_text="出版日期",
)
category = models.SmallIntegerField(
choices=[(item.value, item.name) for item in BookCategoryEnum],
db_comment="图书分类",
help_text="图书分类",
)
total_copies = models.IntegerField(
db_comment="总册数",
help_text="总册数",
)
price = models.DecimalField(
max_digits=10,
decimal_places=2,
db_comment="单价",
help_text="单价(2位小数)",
)
class Meta:
managed = True
db_table = "demo_book"
db_table_comment = "图书信息表"
ordering = ["-id"]
】
###
参考例子:
from rest_framework import serializers
from .models import SystemPost
class SystemPostSerializer(serializers.ModelSerializer):
"""岗位序列化器"""
class Meta:
model = SystemPost
fields = ["id", "code", "name", "sort", "status", "remark", "create_time"]
read_only_fields = ["id", "create_time"]
class SystemPostExportSerializer(serializers.ModelSerializer):
"""岗位导出序列化器"""
class Meta:
model = SystemPost
fields = ["id", "code", "name", "sort", "status"]编写过滤器
针对上一步得到的Django模型,编写过滤器。提示词:
python
根据下面信息,参考例子,编写模型的过滤器
- BaseModel类中定义了create_time字段
- 过滤字段:【 title author publish_date category 】
###
模型定义:
【
class DemoBook(BaseModel):
id = models.BigAutoField(primary_key=True, db_comment="图书ID", help_text="图书ID")
title = models.CharField(
max_length=255,
db_comment="书名",
help_text="书名",
)
author = models.CharField(
max_length=100,
db_comment="作者",
help_text="作者",
)
publisher = models.CharField(
max_length=100,
db_comment="出版社",
help_text="出版社",
)
publish_date = models.DateTimeField(
db_comment="出版日期",
help_text="出版日期",
)
category = models.SmallIntegerField(
choices=[(item.value, item.name) for item in BookCategoryEnum],
db_comment="图书分类",
help_text="图书分类",
)
total_copies = models.IntegerField(
db_comment="总册数",
help_text="总册数",
)
price = models.DecimalField(
max_digits=10,
decimal_places=2,
db_comment="单价",
help_text="单价(2位小数)",
)
class Meta:
managed = True
db_table = "demo_book"
db_table_comment = "图书信息表"
ordering = ["-id"]
】
###
参考例子:
from django_filters import rest_framework as filters
from .models import SystemPost
class SystemPostFilter(filters.FilterSet):
code = filters.CharFilter(
field_name="code", lookup_expr="icontains", label="岗位编码(模糊查询)"
)
name = filters.CharFilter(
field_name="name", lookup_expr="icontains", label="岗位名称(模糊查询)"
)
status = filters.NumberFilter(
field_name="status", lookup_expr="exact", label="岗位状态"
)
class Meta:
model = SystemPost
fields = ["code", "name", "status"]编写视图
针对上一步得到的Django模型,编写视图。提示词:
python
根据下面信息,参考例子,编写模型的视图
- BaseModel类中定义了create_time字段
- tags命名:【 管理后台-demo-图书 】
- 导出字段:【 id title author publish_date category total_copies 】
###
模型定义:
【
class BookCategoryEnum(Enum):
"""图书分类枚举"""
HISTORY = 1 # 历史
ECONOMY = 2 # 经济
CHILDREN = 3 # 儿童
class DemoBook(BaseModel):
id = models.BigAutoField(primary_key=True, db_comment="图书ID", help_text="图书ID")
title = models.CharField(
max_length=255,
db_comment="书名",
help_text="书名",
)
author = models.CharField(
max_length=100,
db_comment="作者",
help_text="作者",
)
publisher = models.CharField(
max_length=100,
db_comment="出版社",
help_text="出版社",
)
publish_date = models.DateTimeField(
db_comment="出版日期",
help_text="出版日期",
)
category = models.SmallIntegerField(
choices=[(item.value, item.name) for item in BookCategoryEnum],
db_comment="图书分类",
help_text="图书分类",
)
total_copies = models.IntegerField(
db_comment="总册数",
help_text="总册数",
)
price = models.DecimalField(
max_digits=10,
decimal_places=2,
db_comment="单价",
help_text="单价(2位小数)",
)
class Meta:
managed = True
db_table = "demo_book"
db_table_comment = "图书信息表"
ordering = ["-id"]
】
###
参考例子:
from rest_framework.permissions import AllowAny
from drf_spectacular.utils import extend_schema
from mars_framework.viewsets.base import CustomModelViewSetNoSimple
from mars_framework.permissions.base import HasPermission
from .models import SystemRole
from .serializers import (
SystemRoleSerializer,
SystemRoleExportSerializer,
)
from .filters import SystemRoleFilter
@extend_schema(tags=["管理后台-system-角色"])
class SystemRoleViewSet(CustomModelViewSetNoSimple):
queryset = SystemRole.objects.all()
serializer_class = SystemRoleSerializer
filterset_class = SystemRoleFilter
action_serializers = {
"export": RoleExportSerializer,
}
action_permissions = {
"create": [HasPermission("system:role:create")],
"destroy": [HasPermission("system:role:delete")],
"update": [HasPermission("system:role:update")],
"retrieve": [HasPermission("system:role:query")],
"list": [HasPermission("system:role:query")],
"export": [HasPermission("system:role:export")],
}
export_name = "角色列表"
export_fields_labels = {
"id": "角色序号",
"name": "角色名称",
"code": "角色标志",
"sort": "角色顺序",
"status": "角色状态",
"data_scope": "数据范围",
}
export_data_map = {
"status": {0: "开启", 1: "关闭"},
"data_scope": {
1: "全部数据权限",
2: "指定部门数据权限",
3: "本部门数据权限",
4: "本部门及以下数据权限",
5: "仅本人数据权限",
},
}