Django之同時新增數據到兩個資料庫表與同時返回兩個表的數據(插拔式)
- 2022 年 10 月 21 日
- 筆記
- django, Python, Python3
from django.db import models
"""
基類,其他類繼承即可獲得對應的欄位
"""
class BaseModel(models.Model):
updated_tm = models.DateTimeField(auto_now=True)
created_tm = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
"""
項目列表
"""
class ProjectList( BaseModel):
project_id = models.AutoField(help_text="項目ID", primary_key=True)
project_name = models.SlugField(max_length=128, help_text="項目名稱", unique=True)
class Meta:
db_table = 'project_list'
verbose_name = '項目列表資訊'
verbose_name_plural = verbose_name
"""
項目詳情
"""
class ProjectDetail(BaseModel):
project_introduction = models.CharField(max_length=128, help_text="用戶簡介")
project_info = models.ForeignKey(to=ProjectList, on_delete=models.DO_NOTHING, db_constraint=False, related_name='user')
class Meta:
db_table = project_detail'
verbose_name = '項目詳情資訊'
verbose_name_plural = verbose_name
一、同時新增數據到兩個資料庫表
from rest_framework import filters, mixins, generics
from rest_framework.viewsets import GenericViewSet
class ProjectListView(mixins.CreateModelMixin, GenericViewSet):
queryset = Account.objects.filter(is_delete=0).all().order_by("-created_tm")
def create(self, request, *args, **kwargs):
# 先定義兩個dict
project_list_dict = {}
project_detail_dict = {}
# 使用 ._meta.get_fields() 獲取資料庫表的全部欄位,再使用field.name獲取全部的欄位名稱,放入各自的dict,不存在的自動忽略掉
for item in request.data.items():
if item in [field.name for field in ProjectList._meta.get_fields()]:
project_list_dict [item[0]] = item[1]
elif item in [field.name for field in ProjectDetail._meta.get_fields()]:
project_detail_dict [item[0]] = item[1]
# 從上面的models可以看到,項目詳情與項目列表兩張表是有關聯的,所以,新增數據的時候,先現在列表,新增成功,生成ID後,再新增詳情,並通過ID與列表數據關聯起來
# 檢查項目是否存在,項目名稱唯一約束,Serializer內設置必填
try:
# 存在就直接失敗
ProjectList.objects.get(project_name=request.data.get('project_ame'))
return xxx
except Account.DoesNotExist:
# 如果不存在,先調用ProjectList表新增數據
try:
project_create = ProjectList.objects.update_or_create(defaults=project_list_dict, project_name=project_list_dict["project_ame"])
# 如果ProjectList表新增數據成功,則通過project_info,在ProjectDetail新增對應的數據
if project_create:
project_id = ProjectList.objects.filter(project_name=project_list_dict["project_ame"]).values('project_id').first()
ProjectDetail.objects.update_or_create(defaults=project_detail_dict, project_info=project_id["project_id"])
return xxx
except Exception as e:
return e
二、同時返回兩個表的數據(插拔式)
- 插拔式的好處:即插即用,需要時才調用,不需要則不調用
- 從ProjectDetail這個models我們可以看到,是沒有定義project_id和project_name的,但我們是與ProjectList關聯的,所以,我們可以通過project_info,返回ProjectList已定義的project_id和project_name
# 我們只需要把ProjectDetail改成以下這樣子就可以了
class ProjectDetail(BaseModel):
project_introduction = models.CharField(max_length=128, help_text="用戶簡介")
project_info = models.ForeignKey(to=ProjectList, on_delete=models.DO_NOTHING, db_constraint=False, related_name='user')
class Meta:
db_table = project_detail'
verbose_name = '項目詳情資訊'
verbose_name_plural = verbose_name
@property
def project_id(self):
return self.project_info.project_id
@property
def project_name(self):
return self.project_info.project_name
# 然後在序列化器內直接調用project_id和project_name即可
class ProjectDetailSerializer(serializers.ModelSerializer):
class Meta:
model = ProjectDetail
fields = ('project_id', 'project_name', 'project_introduction',)