0

我正在使用 MongoDB,连接由 Djongo 提供,过度使用 DRF 来管理对 mi API 的所有请求。

我的数据(个人资料)的结构是这样的

{
    "name" : "profile name",
    "description" : "this is a description",
    "params" : "X1, X2,X3, etc",
    "config" : "CONFIG OF DEVICE",
    "user" : {
        "name" : "user name",
        "middle_name" : "test middle name",
        "last_name" : "test last name",
        "email" : "test@test.com",
        "institute" : {
            "name" : "MIT",
            "place" : {
                "coordinates" : [ 30.0, 101.0,  0.0 ],
                "type" : "Point"
            },
            "country" : "US"
        }
    },
    "place" : {
        "coordinates" : [  90.0, 901.0, 10.0 ],
        "type" : "Point"
    },
    "devices" : [ 
        {
            "name" : "DEVICE 1",
            "verification_code" : "",
            "verificated" : 0,
            "configuration" : "kjk",
            "places" : [ 
                {
                    "coordinates" : [ 30.0,  101.0,  0.0 ],
                    "type" : "Point"
                }, 
                {
                    "coordinates" : [  31.0, 102.0, 1.0 ],
                    "type" : "Point"
                }
            ]
        }
    ]
}

我知道,坐标是错误的,但只是为了测试。

好吧,我将该对象发送到我的视图,然后发送到 ProfileSerializer,这将负责检查嵌入的对象(每个对象都有自己的序列化器)。检查数据后,信息保存没有问题,如下图所示:

截图 2019-03-21 at 14 44 05

但问题是当我尝试时。检索所有配置文件。只是坐标为空,其他嵌入对象检索得很好,只有 Place Object 格式不正确。接下来,我将向您展示响应:

[
{
        "id": 22,
        "name": "profile name",
        "description": "this is a description",
        "params": "X1, X2,X3, etc",
        "config": "CONFIG OF DEVICE",
        "user": {
            "name": "user name",
            "middle_name": "test middle name",
            "last_name": "test last name",
            "email": "test@test.com",
            "institute": {
                "name": "MIT",
                "place": {
                    "coordinates": **null**,
                    "type": "Point"
                },
                "country": "US",
                "created_at": "2019-03-21T20:43:33.928000Z"
            },
            "created_at": "2019-03-21T20:43:33.959000Z"
        },
        "place": {
            "coordinates": **null**,
            "type": "Point"
        },
        "devices": [
            {
                "name": "DEVICE 1",
                "verificated": 0,
                "configuration": "kjk",
                "places": [
                    {
                        "coordinates": **null**,
                        "type": "Point"
                    },
                    {
                        "coordinates": **null**,
                        "type": "Point"
                    }
                ],
                "created_at": "2019-03-21T20:43:33.898000Z"
            }
        ],
        "created_at": "2019-03-21T20:43:33.976000Z"
    }
]

仅对于这个问题,我将描述/显示一个对象的序列化程序,但如果您需要一些信息,我会尽快给您。

楷模

class Place(models.Model):
    coordinates = models.ListField(blank=True, null=True, default=[0.0, 0.0, 0.0])
    type = models.CharField(max_length=10, default="Point")
    objects = models.DjongoManager()

class Profile(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField(default="Without Description")
    params = models.TextField(default="No params")
    config = models.CharField(max_length=200)
    user = models.EmbeddedModelField(
        model_container=User
    )
    place = models.EmbeddedModelField(
        model_container=Place
    )
    devices = models.ArrayModelField(
        model_container=Device
    )
    created_at = models.DateTimeField(auto_now_add=True)
    objects = models.DjongoManager()

序列化器

class PlaceSerializer(serializers.ModelSerializer):
    coordinates = serializers.ListSerializer(
        child=serializers.FloatField(),
    )
    class Meta:
        model = Place
        fields = ('id', 'coordinates', 'type')

class ProfileSerializer(serializers.ModelSerializer):
    user = UserSerializer( )
    place = PlaceSerializer()
    devices = DeviceSerializer( many=True)

    class Meta:
        model = Profile
        fields = ('id', 'name', 'description', 'params', 'config',
                  'user', 'place', 'devices', 'created_at')
        depth=8

    def create(self, validated_data):
        # get principal fields
        user_data = validated_data.pop('user')
        **place_data = validated_data.pop('place')**
        devices_data = validated_data.pop('devices')

        # get nested fields

        # devices nested fields
        devices = []
        for device in devices_data:
            places = []
            places_data = device.pop('places')
            for place in places_data:
                places.append( **Place(coordinates=place['coordinates'], type=place['type'])** )

            device['places'] = places
            devices.append( Device.objects.create(**device) )
        validated_data['devices'] = devices
        # user nested fields
        institute_data = user_data.pop('institute')
        place = institute_data.pop('place')
        institute_data['place'] = Place(coordinates=place['coordinates'], type=place['type'])
        user_data['institute'] = Institute.objects.create(**institute_data)
        validated_data['user'] = User.objects.create(**user_data)
        profile = Profile.objects.create(**validated_data)
        return profile

我已经以多种方式定义了 PlaceSerializer,但它们都得到相同的结果,下面描述这些方式

情况1

class PlaceSerializer(serializers.ModelSerializer):
    coordinates = serializers.ListSerializer(
        child=serializers.FloatField(),
    )

案例二

class CoordinatesSerializer(serializers.ListSerializer):
        child=serializers.FloatField()

class PlaceSerializer(serializers.ModelSerializer):
    coordinates = CoordinatesSerializer()

案例 3

class PlaceSerializer(serializers.ModelSerializer):
    coordinates = serializers.ListField(
        child=serializers.FloatField()
    )

案例 4

class PlaceSerializer(serializers.ModelSerializer):
    coordinates = serializers.ListField()

案例 5

class PlaceSerializer(serializers.ModelSerializer):
    coordinates = serializers.ListSerializer()
#gives error for child is not present

我更改了类型、CharField、IntegerField、FloatField 等,结果相同。

我已经完成的另一个测试是附加到序列化程序的方法 create、update、to_representation、to_internal_value,所有这些都是为了以更好的方式管理将保存或检索但任何工作的信息。另一个好奇心,如果我添加一个像 [10,90,1] 这样的简单 Listfield,则相反,当这个 ListField 位于 Place Objects 中时,可以毫无问题地保存和检索

如果你知道如何解决这个问题,我会很高兴你。

4

0 回答 0