'''
2、完成汽车系统表模型的建立(表关系自己设计)
汽车表、汽车商家表、赞助商表、赞助商详情表
'''
from django.db import models
# 汽车表、汽车商家表、赞助商表、赞助商详情表
class BaseModel(models.Model):
is_delete = models.BooleanField(default=False)
createdTime = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
class Car(BaseModel):
name = models.CharField(max_length=64)
price = models.DecimalField(max_digits=10, decimal_places=2)
brand = models.ForeignKey(to='Brand', db_constraint=False, null=True, on_delete=models.SET_NULL, related_name='cars')
sponsors = models.ManyToManyField(to='Sponsor', db_constraint=False, related_name='cars')
@property
def brand_name(self):
return self.brand.name
@property
def sponsor_list(self):
sponsor_list_temp = []
for sponsor in self.sponsors.all():
sponsor_dic = {
'name': sponsor.name
}
try:
sponsor_dic['phone'] = sponsor.detail.phone
except:
sponsor_dic['phone'] = '未知'
sponsor_list_temp.append(sponsor_dic)
return sponsor_list_temp
class Meta:
verbose_name_plural = '汽车'
def __str__(self):
return self.name
class Brand(BaseModel):
name = models.CharField(max_length=64)
class Meta:
verbose_name_plural = '品牌'
def __str__(self):
return self.name
class Sponsor(BaseModel):
name = models.CharField(max_length=64)
@property
def phone(self):
try:
return self.detail.phone
except:
return "无"
class Meta:
verbose_name_plural = '赞助商'
def __str__(self):
return self.name
class SponsorDetail(BaseModel):
phone = models.CharField(max_length=11)
sponsor = models.OneToOneField(to=Sponsor, db_constraint=False, on_delete=models.CASCADE, related_name='detail')
class Meta:
verbose_name_plural = '赞助商详情'
def __str__(self):
try: # 连表可能会出现问题,所以要异常处理
return self.sponsor.name + '的详情'
except:
return super().__str__()
'''
3、完成汽车系统的接口编写
汽车表:八大接口(不需要写put方法)
汽车商家表:八大接口(不需要写put方法)
'''
class CarModelViewSet(ModelViewSet):
queryset = models.Car.objects.filter(is_delete=False).all()
serializer_class = serializers.CarModelSerializer
# 分析:从实际开发角度分析不合理点
# 1)没有群增,群整体改,群局部改,群删四个接口
# 2)删除操作视图集默认走的destroy方法是将资源从数据库中删除,通常一个做字段is_delete字段修改表示删除
# 3)响应的结果只有数据,没有数据状态码和状态信息
# 解决1,
# 群整体改,群局部改,全删三个接口可以独立成三个方法
# def many_update(self, request, *args, **kwargs):
# return APIResponse(msg='这个地方是群整体改,你会写!')
def many_partial_update(self, request, *args, **kwargs):
return APIResponse(msg='这个地方是群局部改,你会写!')
def many_destroy(self, request, *args, **kwargs):
return APIResponse(msg='这个地方是群删,你会写!')
# 群增与单增必须公用一个接口,都要走create方法 - 重写create方法,用逻辑进行拆分
def create(self, request, *args, **kwargs):
request_data = request.data
if isinstance(request_data, list):
car_ser = self.get_serializer(data=request_data, many=True)
car_ser.is_valid(raise_exception=True)
car_obj = car_ser.save()
return APIResponse(msg='群增成功', results=self.get_serializer(car_obj, many=True).data)
return super().create(request, *args, **kwargs)
# 解决2,destroy方法是完成is_delete字段值修改 - 重写destroy方法,自定义实现体
def destroy(self, request, *args, **kwargs):
car_obj = self.get_object()
car_obj.is_delete = True
car_obj.save()
return APIResponse(msg='删除成功')
# 解决3,让群查有状态码和状态信息 - 重写list方法
def list(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
return APIResponse(results=response.data)