【问题标题】:Issue with Serializing list in Django framework and Vue JSDjango框架和Vue JS中的序列化列表问题
【发布时间】:2020-01-17 06:27:28
【问题描述】:

我想在更新期间序列化列表,例如使用“cuisine”在用户配置文件下方进行补丁调用来更新

接电话

**http://localhost:8000/api/v1/chef/rayees/details**


    {
        "user": {
            "pk": 2,
            "username": "rayees",
            "email": "rayees.xxxx@yahoo.co.in",
            "first_name": "Rayees",
            "last_name": "xxxx",
            "user_type": "Chef"
        },
        "first_name": "Rayees",
        "last_name": "xxx",
        "bio": "TEST BIO",
        "chef_cost": "10.00",
        "serve_type": "Non-Veg",
        "cuisine": [
            "South Indian", 
            "North Indian"
        ]

    }

这是我的序列化程序类,我认为“serializers.SerializerMethodField()”是只读的,并且“cuisine”在补丁操作期间没有得到更新,如果想同时序列化get和补丁,我应该在这里做什么

class ChefProfileDetails(serializers.ModelSerializer):
    chef_date = AvailabilityDetails(many=True, read_only=True)
    first_name = serializers.CharField(source='user.first_name', read_only=True)
    last_name = serializers.CharField(source='user.last_name', read_only=True)
    cities = CitySerializer(many=True, read_only=True)
    cuisine = serializers.SerializerMethodField()
    user = UserDetailsSerializer(read_only=True)

    class Meta:
        model = Chef
        fields = ('user', 'first_name', 'last_name', 
                  'bio', 'chef_cost', 'serve_type', 'cuisine', 'chef_date', 'cities')

    def get_cuisine(self, obj):
        cuisine_list = []
        for i in obj.cuisine.all():
            cuisine_list.append(i.cuisine)
        return cuisine_list

【问题讨论】:

    标签: django django-models vuejs2 django-rest-framework django-views


    【解决方案1】:

    Chef 和 Cuisine 模型之间存在一对多的联系,因此请尝试使用 RelatedField:

    class ChefProfileDetails(serializers.ModelSerializer):
        cuisines = serializers.RelatedField(many=True)
    
        class Meta:
            model = Chef
            fields = ('user', 'first_name', 'last_name', 
                      'bio', 'chef_cost', 'serve_type',
                      'cuisine', 'chef_date', 'cities')
    
    

    我认为应该可以。

    【讨论】:

    • 它失败了```文件“/Users/rayeesn/Documents//venv/lib/python3.7/site-packages/rest_framework/fields.py”,第589行,在to_representation field_name=self .field_name, NotImplementedError: RelatedField.to_representation() 必须为 field 实现。如果你不需要支持写操作,你可能想要继承ReadOnlyField。 2020-01-17 07:17:57,105 raven.contrib.django.client.DjangoClient INFO Raven 未配置(禁用日志记录)。请参阅文档以获取更多信息。 ```
    • @RayeesNamathponnan 哎呀,我认为它应该是 StringRelatedField 然后:D
    • 我的美食模型是多对多美食 = models.ManyToManyField('Cuisine')
    【解决方案2】:

    您可以通过定义一个自定义字段来实现您所需要的,然后分别在to_representation()to_internal_value() 中提供您对getter 和setter 的定义。

    代码:

    class CuisineField(serializers.Field):
        def to_representation(self, value):
            cuisine_list = []
            for i in value:
                cuisine_list.append(i.cuisine)
            return cuisine_list
    
    
        def to_internal_value(self, data):
            ""
             Do whatever you want to do in patch
            ""
    

    然后在您的序列化程序中将美食定义为 CuisineField

    cuisine = CuisineField()

    有关自定义字段,请参阅 DRF docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 2015-04-28
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多