Django Rest框架中的自定义词典

我试图得到这样一个JSON。

{
        "course_name": "name",
        "course_id": "44",
        "icon": 1,
        "published_at":"2014-10-23T03:15:54Z",
        "skills": {
            "self":[
                {
                    "skill_name": "Cuidado Emocional",
                    "points": 5,
                },
                {
                    "skill_name": "Cuidado Físico",
                    "points": 5,
                }
            ],
            "abilities":[
                {
                    "skill_name": "Principios Básicos",
                    "points":5,
                },
                {
                    "skill_name": "Administración",
                    "points": 5,
                }
            ]

        },

在技能字典中,有一个字段是type_hability, 我想通过type_hability来分组:

这是我的模型

class Ability(models.Model):
    ability = models.CharField("Habilidad", max_length=255)
    ability_type = models.CharField('Tipo', max_length=2, choices=ABILITY_TYPE)
    course = models.ForeignKey(Course, related_name='abilities')
    points = models.PositiveIntegerField()
    active = models.BooleanField("Activo", default=True)
    created_at = models.DateTimeField("Creado en", auto_now_add=True)
    updated_at = models.DateTimeField("Actualizado a las", auto_now=True)

    class Meta:
        verbose_name = u'Habilidad'
        verbose_name_plural = u"Habilidades"
        ordering = ["-created_at"]

    def __unicode__(self):
        return self.ability

我试着创建一个像这样的Serializer。

class AbilitySerializer(serializers.ModelSerializer):
    dictAutoCuidado = {}
    dictHabilidades = {}

    class Meta:
        model = Ability
        fields = ('ability', 'points')

    def to_native(self, value):
        if value.ability_type == 'AU':
            self.dictAutoCuidado.update({
                'skill_name': value.ability,
                'points': value.points
            })
        else:
            self.dictHabilidades.update({
                'skill_name': value.ability,
                'points': value.points
            })

        return {
            'self': self.dictAutoCuidado,
            'ability': self.dictHabilidades
        }


class CourseListSerializer(serializers.ModelSerializer):
    author = serializers.RelatedField(source='author.name')
    objectives = serializers.RelatedField(many=True)
    abilities = AbilitySerializer(many=True)

    class Meta:
        model = Course
        fields = (
            'id',
            'title',
            'description',
            'objectives',
            'abilities',
        )

但当我重新加载我的页面时,JSON的内容是

{
    "id": 1, 
    "title": "La vida es bella", 
    "created_at": "2014-10-23T02:06:21Z", 
    "abilities": [
        {
            "self": {
                "points": 100, 
                "skill_name": "Habilidad 1"
            }, 
            "ability": {
                "points": 250, 
                "skill_name": "Habilidad 2"
            }
        }, 
        {
            "self": {
                "points": 100, 
                "skill_name": "Habilidad 1"
            }, 
            "ability": {
                "points": 250, 
                "skill_name": "Habilidad 2"
            }
        }, 
        {
            "self": {
                "points": 100, 
                "skill_name": "Habilidad 1"
            }, 
            "ability": {
                "points": 250, 
                "skill_name": "Habilidad 2"
            }
        }
    ]
}

而且能力没有按照我的要求进行分组。你能告诉我我做错了什么,有什么办法可以解决这个问题吗?我试过用一些queryset,但没有用。我试着用一些queryset覆盖 启动 方法,但我没有找到按照我的要求返回自定义听写组的方法。

谢谢。

解决方案:

我认为这些字段应该是

dictAutoCuidado = serializers.DictField()
dictHabilidades = serializers.DictField()

链接。https:/www.django-rest-framework.orgapi-guidefields#dictfield

给TA打赏
共{{data.count}}人
人已打赏
未分类

如何打印双变量中包含的正确数字?

2022-9-9 18:12:01

未分类

传递了所有的值,仍然得到这个错误。类型错误:add_info()缺少1个所需位置参数:'writer'。

2022-9-9 18:12:03

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索