【问题标题】:Nested ModelSerializer嵌套模型序列化器
【发布时间】:2018-07-05 13:58:57
【问题描述】:

我正在尝试通过 Django Rest Framework 提供嵌套的 json 文件。我一直在阅读不同的方式来嵌套我的序列化程序,但我没有让它工作。

我不知道如何自己创建模型序列化器,而不是直接从我的模型中提取数据...

我的错误:

/listings.json 处的 AttributeError 在尝试 获取序列化程序 TestSerializer 上字段 coordinates 的值。 序列化器字段可能命名不正确并且不匹配任何 Test 实例上的属性或键。原始异常文本是: “测试”对象没有“坐标”属性。

我的序列化代码:

from rest_framework import serializers
from rentlistings.models import Test


class coordinatesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Test
        fields = ('latitude', 'longitude')


class propertiesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Test
        fields = ('price', 'price', 'yields',
                  'num_floor', 'num_rooms', 'elevator', 'garage',
                  'balcony_size', 'garden_area', 'parking', 'terass',
                  'loggia', 'cellar', 'hash_id')


class TestSerializer(serializers.Serializer):
coordinates = coordinatesSerializer(many=True, read_only=True)
properties = propertiesSerializer(many=True, read_only=True)

    class Meta:
        model = Test
        fields = ('coordinates', 'properties')

我的意见.py

from rentlistings.models import Test
from rentlistings.serializers import TestSerializer
from rest_framework import generics
# Create your views here.


class test_list(generics.ListCreateAPIView):
        queryset = Test.objects.all()
        serializer_class = TestSerializer´

使用@aircraft cmets 更正后,我没有收到错误,但API json 中没有包含任何内容:

models.py

from django.db import models


# Create your models here.
class Test(models.Model):
    latitude = models.FloatField(blank=True, null=True)
    longitude = models.FloatField(blank=True, null=True)
    hash_id = models.BigIntegerField(primary_key=True, blank=True)
    price = models.BigIntegerField(blank=True, null=True)
    floor = models.TextField(blank=True, null=True)
    garden_area = models.FloatField(blank=True, null=True)
    parking = models.TextField(blank=True, null=True)
    terass = models.TextField(blank=True, null=True)
    loggia = models.TextField(blank=True, null=True)
    cellar = models.TextField(blank=True, null=True)
    elevator = models.NullBooleanField()
    garage = models.TextField(blank=True, null=True)
    balcony_size = models.TextField(blank=True, null=True)
    date = models.DateTimeField(blank=True, null=True)
    last_seen = models.DateTimeField(blank=True, null=True)
    num_floor = models.BigIntegerField(blank=True, null=True)
    num_rooms = models.BigIntegerField(blank=True, null=True)
    yields = models.FloatField(blank=True, null=True)


    class Meta:
        managed = False
        db_table = 'test'

【问题讨论】:

    标签: python json django django-rest-framework


    【解决方案1】:

    在您的TestSerializer 中,您应该添加如下参数:

    class TestSerializer(serializers.Serializer):
        coordinates = coordinatesSerializer(many=True, read_only=True)
        properties = propertiesSerializer(many=True, read_only=True)
    
        class Meta:
            model = Test
            fields = ('coordinates', 'properties')
    

    【讨论】:

    • 您好,非常感谢!这实际上消除了错误,但不知何故它不包含任何信息(只是一个空的 json,其中包含我期望的对象数量,但里面什么都没有)......有什么想法吗?当我将 Serializer 用于合并的嵌套端序列化器时,这可能与我对其他两个使用 ModelSerializer 有关吗?感谢您的帮助!
    • 显示模型。可能外键中没有related_name。
    • @Svarto 大错特错。那里没有外国模特!如果你想使用嵌套序列化程序。它应该基于前键。
    • 哦,好吧,我很困惑。我只想构建我的 json 文件,使其具有一个节点下的坐标和另一个节点下的属性 - 类似于 GeoJSON。我会阅读外键,谢谢你的提示!
    猜你喜欢
    • 1970-01-01
    • 2014-07-26
    • 2017-11-15
    • 2019-06-02
    • 2016-09-03
    • 2020-09-29
    • 2023-03-08
    • 2011-10-06
    • 2020-02-04
    相关资源
    最近更新 更多