Python Django rest-framework序列化器省略字段

时间:2015-09-22 15:15:15

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

我在使用django rest-framework的python2.7时遇到了问题。当我序列化我的JSON数据时,序列化程序省略了一个字段,我不明白为什么。这是一些细节。

缺少的字段是" country"。当我在/ campaigns /:id

上执行POST或PUT请求时
class CampaignSerializer(serializers.HyperlinkedModelSerializer):
    created_by = UserFullSerializer(read_only=True)
    country = CountrySerializer(read_only=True)

    class Meta:
        model = Campaign
        fields = ('id', 'created_by', 'name', 'media', 'status', 'begin', 'end', 'country')


class CampaignFullSerializer(serializers.HyperlinkedModelSerializer):
    client = ClientSerializer(read_only=True)
    collection = CollectionSerializer(read_only=True)
    created_by = UserFullSerializer(read_only=True)
    updated_by = UserFullSerializer(read_only=True)
    channels = ChannelSerializer(read_only=True, many=True)
    country = CountrySerializer(read_only=True)

    class Meta:
        model = Campaign
        fields = ('id',
                  'client',
                  'name',
                  'media',
                  'status',
                  'begin',
                  'end',
                  'created_at',
                  'created_by',
                  'updated_at',
                  'updated_by',
                  'collection',
                  'channels',
                  'country')

class CountrySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Country
        fields = ('id', 'name', 'code')


class Country(models.Model):
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=255)

class Campaign(models.Model):
    name = models.CharField(max_length=255)
    media = models.IntegerField(choices=constant.MEDIA_CHOICES, default=0)
    status = models.IntegerField(choices=constant.STATUS_CHOICES, default=2)
    created_at = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User, blank=True, null=True, related_name="created_by")
    updated_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_by = models.ForeignKey(User, blank=True, null=True, related_name="updated_by")
    client = models.ForeignKey(client.Client)
    begin = models.DateField(blank=True, null=True)
    end = models.DateField(blank=True, null=True)
    collection = models.ForeignKey(collection.Collection, blank=True, null=True)
    country = models.ForeignKey(country.Country, blank=True, null=True)
    mediaplan = models.ForeignKey(mediaplan.Mediaplan, blank=True, null=True, default=None)
    channels = models.ManyToManyField(channel.Channel)

当我使用以下JSON对/ campaign / id进行POST时,除了国家/地区字段外,一切正常。

{
    ...
    "channels": [],
    "country": {
        "id": 74,
        "name": "France",
        "code": "FR"
}

当我打印request.data时,在控制器端,我得到了所有字段。我没有覆盖控制器的create方法。

{
    ...
    u'country': {u'code': u'AL', u'id': 3, u'name': u'Albania'}
}

我的控制器如下:

class CampaignViewSet(viewsets.ModelViewSet):
    queryset = Campaign.objects.all()
    serializer_class = CampaignSerializer

    def create(self, request):
        logger.info(request.data)
        return super(CampaignViewSet, self).create(request, *args, **kwargs)

我试图覆盖我的CountrySerializer的create方法,当我打印validated_data的内容时,OrderedDict中缺少country字段..

class CountrySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Country
        fields = ('id', 'name', 'code')

        def create(self, validated_data):
            logger.info(validated_data)

我真的输了,我无法找到我的错误,也许你会。谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

默认情况下,CountrySerializer只作为嵌套序列化程序读取(每http://www.django-rest-framework.org/api-guide/relations/#nested-relationships),因此您必须覆盖POST序列化程序的POST / PUT创建/更新方法。您已尝试在Country序列化程序上覆盖它。