使用Django Rest Framework访问'ManyRelatedManager'上的不同字段?

时间:2015-02-16 10:29:54

标签: python json django serialization django-rest-framework

我正在尝试访问ManyToMany链接的直通表上的字段,以通过Django Rest框架序列化为JSON。

我的模型涉及多对多:

class Mage(models.Model):
    arcana = models.ManyToManyField('ArcanumAbility', through='CharacterArcanumLink', related_name='mage_by_arcana')

class ArcanumAbility(models.Model):

    class Arcana(AutoNumber):
        FATE = ()
        MIND = ()
        SPIRIT = ()
        DEATH = ()
        FORCES = ()
        TIME = ()
        SPACE = ()
        LIFE = ()
        MATTER = ()
        PRIME = ()

    arcanum = EnumField(Arcana)

    class Meta:
        verbose_name_plural = "Arcana Abilities"

    def __str__(self):
        return self.arcanum.label

class CharacterArcanumLink(Trait):
    PRIORITY_CHOICES = (
        (0, 'Unassigned'), (1, 'Ruling'), (2, 'Common'), (3, 'Inferior')
    )
    priority = models.PositiveSmallIntegerField(
        choices=PRIORITY_CHOICES, default=0)
    mage = models.ForeignKey('Mage')
    arcana = models.ForeignKey('ArcanumAbility')

    class Meta:
        unique_together = ('mage', 'arcana')

    def __str__(self):
        return self.arcana.arcanum.label

Trait mixin提供current_value 的位置

要将上述关系序列化为我的JSON,我在序列化器上尝试了这两个模式:

class CharacterArcanumLinkSerializer(serializers.ModelSerializer):

    class Meta:
        model = CharacterArcanumLink
        fields = ('current_value', 'arcana')

class MageSerializer(serializers.ModelSerializer):
    arcana = CharacterArcanumLinkSerializer()
    ....
    class Meta:
        model = Mage
        fields = (...., 'arcana', ....)
        depth = 1

但是这给了我这个错误:

AttributeError at /mages
'ManyRelatedManager' object has no attribute 'arcana'

来自(最终):

C:\Python34\lib\site-packages\rest_framework\fields.py in get_attribute
        if instance is None:
            # Break out early if we get `None` at any point in a nested lookup.
            return None
        try:
            if isinstance(instance, collections.Mapping):
                instance = instance[attr]
            else:
                instance = getattr(instance, attr) ...
        except ObjectDoesNotExist:
            return None
        if is_simple_callable(instance):
            instance = instance()
    return instance
▼ Local vars
    Variable:   Value
    instance:   <django.db.models.fields.related.create_many_related_manager.<locals>.ManyRelatedManager object at 0x0000000004E4D4A8>
    attr:    'arcana'
    attrs:    ['arcana']

(问题:我需要从我的ManyRelatedManager到它的字段有什么技巧?)

我还尝试过不指定特殊的序列化程序,只在我的字段中使用'arcana',并从模型中提取它。这导致了这个错误:

TypeError at /mages
<Arcana.FATE: 1> is not JSON serializable

1来自ArcanumAbility上的PK,而不是直通表上的值。这里的问题是Mage类有一个指向'ArcanumAbility'模型的M2M字段,所以DRF尝试做的就是序列化它上面的Enum。

那么如果我想要一个从Mage到ArcanumAbility的所有关系的JSON字典和来自表格的数据,我应该使用什么方法?

回应Mark R.,我希望看起来像这样:

....
"arcanum": {
           "Fate": 2,
           "Spirit": 0,
           "Mind": 3,
           ....
           }

希望这是一个足够清晰的样本。

2 个答案:

答案 0 :(得分:4)

如上所述,如果添加related_name =&#34; linked_arcana&#34;在CharacterArcanumLink类的mage字段中,您应该可以执行以下操作:

class MageSerializer(serializers.ModelSerializer):
    arcana = serializers.SerializerMethodField()
    def get_arcana(self, obj):
         if obj:
             return {str(x): x.current_value for x in obj.linked_arcana.all()}

答案 1 :(得分:0)

我得到了这样的工作:

arcana = serializers.SerializerMethodField()

def get_arcana(self, obj):
    if obj:
        return {str(link): link.current_value
                for link in CharacterArcanumLink.objects.filter(mage=obj)}

受到this回答的强烈启发。