【问题标题】:How to join multiple Model in Django Rest Framework serializer如何在 Django Rest Framework 序列化程序中加入多个模型
【发布时间】:2020-07-26 15:44:00
【问题描述】:

在 Django 中,我有以下模型。

class Property(models.Model):
    address1 = models.CharField(max_length=512)
    address2 = models.CharField(max_length=128, blank=True, null=True)
    property_type = models.ForeignKey('PropertyType', models.DO_NOTHING, null=True, blank=True, default=None)
    geo_info = models.ForeignKey(GeoInfo, models.DO_NOTHING)
    
    class Meta:
        indexes = [
            models.Index(fields=['address1', ]),
        ]
        db_table = 'property'

class PropertyType(models.Model):
    name = models.CharField(max_length=128, blank=True, null=True)
    category = models.CharField(max_length=45, blank=True, null=True)
    color = models.CharField(max_length=45, blank=True, null=True)

    class Meta:
        db_table = 'property_type'
        indexes = [
            models.Index(fields=['name', ]),
        ]

class GeoInfo(models.Model):
    zipcode = models.CharField(max_length=25, blank=True, null=True)
    city = models.CharField(max_length=45, blank=True, null=True)
    state = models.CharField(max_length=45, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, null=True)

    class Meta:
        db_table = 'geo_info'
        indexes = [
            models.Index(fields=['zipcode', ]),
            models.Index(fields=['city', ]),
        ]
        
    def __str__(self):
        return '%s %s' % (self.zipcode, self.city)

我正在尝试使用 Django REST Framework 来序列化 PROPERTY 模型,如下所示。

class GeoSerializer(serializers.ModelSerializer):
    class Meta:
        model = GeoInfo
        fields = ['zipcode', 'city', 'state']


class PropertyTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = PropertyType

class PropertySerializer(serializers.ModelSerializer): 
    address_geo = GeoSerializer(many=False, read_only=True)
    prop_type = PropertyTypeSerializer(many=True, read_only=True)

    class Meta:
        model = Property
        fields = ['id', 'address1', 'address_geo', 'prop_type']

我对每个项目的预期结果是这样的

{
        "id": 1,
        "address1": "test address",
        "geo_info":{
           "zipcode":"12121",
           "city":"12121",
           "state":"12121",
           },
        "property_type": "unknown",
       
    }

目前,我正在获取类似的属性数据

{
        "id": 1,
        "address1": "test address"
}

那么你能帮我弄清楚如何序列化这些模型吗?

另外,如果您能帮助我如何展平结果并尽可能避免嵌套节点,那就太好了

【问题讨论】:

标签: python django django-models django-rest-framework


【解决方案1】:

您的问题并不完全清楚。你要求不要嵌套。这可能会对你有所帮助 -

class PropertySerializer(serializers.ModelSerializer):
    zipcode=serializers.SerializerMethodField()
    city=serializers.SerializerMethodField()
    state=serializers.SerializerMethodField()
    class Meta:
        model = Property
        fields = ('id','address1','zipcode','city','state','property_type')

    def get_zipcode(self,instance):
        return instance.geo_info.zipcode
    def get_city(self,instance):
        return instance.geo_info.city
    def get_state(self,instance):
        return instance.geo_info.state

【讨论】:

  • 请注意,序列化方法字段没有属于该字段的默认验证器。最好使用the source
猜你喜欢
  • 1970-01-01
  • 2019-02-06
  • 2013-11-27
  • 2014-11-12
  • 2017-02-08
  • 2018-11-21
  • 2015-05-28
  • 2017-07-21
  • 1970-01-01
相关资源
最近更新 更多