zoukankan      html  css  js  c++  java
  • drf 序列化外键表中的 数据

    models.py

    class Counties(BaseModel):
        countname = models.CharField(max_length=128,
                                     verbose_name="国家名称",
                                     null=True,
                                     blank=True)
        phoneareacode = models.IntegerField(verbose_name="电话区号",
                                            null=True,
                                            blank=True)
        code = models.IntegerField(verbose_name="国家code",
                                   null=True,
                                   blank=True)
        county_num = models.CharField(verbose_name="标号",
                                      max_length=10,
                                      null=True,
                                      blank=True)
    
        class Meta:
            verbose_name = "国家表"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.countname
    
    
    class Provinces(BaseModel):
        provincesname = models.CharField(max_length=128,
                                         verbose_name="城市名字")
        # counties = models.OneToOneField(to="Counties",
        #                                 verbose_name="所属国家",
        #                                 related_name="coun",
        #                                 on_delete=models.CASCADE,
        #                                 null=True,
        #                                 blank=True)
        counties = TenantForeignKey(to="Counties",
                                    verbose_name="所属国家",
                                    related_name="coun",
                                    on_delete=models.CASCADE,
                                    null=True,
                                    blank=True)
        provinces_num = models.CharField(verbose_name="编号",
                                         max_length=10,
                                         null=True,
                                         blank=True)
    
        class Meta:
            verbose_name = "省份表"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.provincesname
    
    
    class City(BaseModel):
        cityname = models.CharField(max_length=128,
                                    verbose_name="城市名字",
                                    null=True,
                                    blank=True)
        provinces = TenantForeignKey(to="Provinces",
                                     verbose_name="所属省份",
                                     related_name="prov",
                                     on_delete=models.CASCADE,
                                     null=True,
                                     blank=True)
        city_num = models.CharField(max_length=10,
                                       verbose_name="城市标号",
                                       null=True,
                                       blank=True)
    
    
        class Meta:
            verbose_name = "城市表"
            verbose_name_plural = verbose_name
    
        def __str__(self):
            return self.cityname

    serialzier.py

    from models import (Jobs,
                        Aivideomodel,
                        Aiability,
                        Question,
                        Candidate,
                        InvitationJobInterviewForm,
                        Jobtype,
                        Functions,
                        City,
                        Provinces,
                        Counties)
    class JobTypeSerializer(serializers.ModelSerializer):
    functions_name = serializers.CharField(source='functions.function_name')

    class Meta:
    model = Jobtype
    fields = ('id', 'jobtype_name', 'functions', 'functions_name')


    class CitySerializer(serializers.ModelSerializer):
    provinces_name = serializers.CharField(source='provinces.provincesname') # 跨一张表序列化省的名字
    count_id = serializers.IntegerField(source='provinces.counties.id') # 跨两张表序列化国家的ID
    count_name = serializers.CharField(source='provinces.counties.countname') # 跨两张表序列化国家的名字 注意 source='' 指定来源那张表那个字段 注意这些外键关联的表model必须要导入进来

    class Meta:
    model = City
    fields = ('id', 'cityname', 'provinces_id', 'provinces_name', 'count_id', 'count_name')
     
  • 相关阅读:
    H5中https://j.map.baidu.com/e7/M6hJ的地址是如何生成的
    转义符
    vue-json-excel导出数字字符串转为科学计数法问题和文件格式问题
    高级JavaScript(4)RegExp,正则表达式
    js7种数据类型
    gulp自动化打包及静态文件自动添加版本号
    解决——CSS :before、:after ,当content使用中文时有时候会出现乱码
    【移动端】移动端点击可点击元素时,出现蓝色默认背景色
    我去!爬虫遇到字体反爬,哭了
    实战|教你用Python玩转Mysql
  • 原文地址:https://www.cnblogs.com/tangda/p/12369632.html
Copyright © 2011-2022 走看看