0
我想我Django項目從mailchimp API進行整合,將用戶添加到列表,我需要生成的格式如下一些JSON:讓嵌套值串行Django的休息串行
{
"email_address": "EMAIL",
"status": "subscribed",
"merge_fields": {
"FNAME": "FIRST_NAME",
"LNAME": "SURNAME"
},
可悲的是,我正在與嵌套的merge_field進行一些鬥爭。我希望下面的工作:
class MergeSerializer(serializers.Serializer):
FNAME = serializers.SerializerMethodField('get_first_name')
LNAME = serializers.SerializerMethodField('get_surname')
def get_first_name(self, obj):
return obj.first_name
def get_surname(self, obj):
return obj.surname
class CreateContactSerializer(serializers.Serializer):
email_address = serializers.EmailField()
status = serializers.SerializerMethodField('get_alternative_status')
merge_fields = MergeSerializer(read_only=True)
def get_alternative_status(self, obj):
return "subscribed"
這隻能產生一些JSON與EMAIL_ADDRESS和狀態,並完全忽略merge_fields。幾個小時後,我完全不知道下一步該做什麼。有人知道如何解決這個問題嗎?
因爲我認爲,對於棉花糖框架的文件是一個更清楚一點我也有自己的包試了一下,這可是returend完全相同的結果(這樣無視我merge_fields):
class MergeFieldsSchema(Schema):
FNAME = fields.String(attribute="first_name")
LNAME = fields.String(attribute="surname")
class CreateContactSerializer(Schema):
merge_fields = fields.Nested(MergeFieldsSchema)
email_address = fields.String()
status = fields.Constant("subscribed")
你是如何連接的數據模型,以串行和看法......? – Satyajeet