如何在Django中添加和序列化现有模型的其他字段

时间:2015-09-08 11:19:34

标签: python django django-rest-framework python-social-auth

我在Django项目中使用django-rest-frameworkpython-social-auth

以下是我项目中UserSocialAuth模型的序列化程序类

class SocialAuthSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.CharField()

    class Meta:
        model = UserSocialAuth
        fields = ('id', 'provider')

然后我想在Serializer之上增加一个字段UserSocialAuth.extra_data['login'],传统方式应该是

class UserSocialAuth(AbstractUserSocialAuth):
    def login:
        return self.extra_data['login']

class SocialAuthSerializer(serializers.HyperlinkedModelSerializer):
    login = serializers.CharField(source='login')
     ...

        fields = ('id', 'provider', 'login')

问题是UserSocialAuth属于python-social-auth,我必须直接更改python-social-auth app的代码才能添加def login:,那么我该如何添加额外的UserSocialAuth字段到现有模型python-social-auth,而不触及<xsl:template match="ccb:correlationId" priority="5"> <xsl:copy> <!-- in case you have attributes (not in your source) --> <xsl:apply-templates select="@*" /> <!-- in case you need to keep current value as well --> <xsl:value-of select="." /> <!-- current date/time --> <xsl:value-of select="current-dateTime()" /> </xsl:copy> </xsl:template> 的代码。

1 个答案:

答案 0 :(得分:0)

我发现我可以在这里使用Pair,无需更改原始类while True: a=str(raw_input(...)) if a=="whatever": break # other responses to a ,只需在序列化程序中添加一个字段,如下所示:

SerializerMethodField