zoukankan      html  css  js  c++  java
  • Day 74 作业 汽车系统

    '''
    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)
    
  • 相关阅读:
    张艾迪(创始人):AOOOiA.global因梦想而诞生
    openx中如何使用site-variable(参数)限制(关键词)广告显示
    那些支持图片外链的免费相册
    php获取html纯文本,解决编辑器手动键入空格造成的无意义空白字符(空值问题)
    ios7迎来完美越狱,果粉狂欢!
    如何记录搜索引擎爬行记录php版
    购物返现网址大全
    jquery .post .get中文参数乱码解决方法详解
    【BUG】wego购物分享系统未登陆分享宝贝时查看宝贝自动新增产品数据
    让wego微购购物分享系统采集拍拍数据功能之腾讯paipai功能采集插件
  • 原文地址:https://www.cnblogs.com/2222bai/p/12120711.html
Copyright © 2011-2022 走看看