在serializer.validated_data中显示ID

时间:2016-06-24 14:27:39

标签: python django django-rest-framework

我有嵌套关系,当我需要在serializer.validated_data中公开序列化程序的ID时,但这不起作用,这里我将展示我的序列化程序的一个例子:

class AdsImagesSerializer(serializers.ModelSerializer):

   image = serializers.ImageField(use_url=True,allow_empty_file=True)

   class Meta:
       model = AdsImages
       fields = ('id','image','ads',)



class ADSerializer(serializers.ModelSerializer):
    adsImages = AdsImagesSerializer(read_only=True,many=True)

    class Meta:
      model = ADS
      fields = ('id','headline','description_1','description_2','display_url','final_url','mobile_url','adsImages','advertiser',)


class CampaignSerializer(serializers.ModelSerializer):

    advertiser = AdvertiserProfile(read_only=True,required=False)
    ads = ADSerializer(many=True)

    class Meta:
      model = Campaign
      fields = ('id','name','type','start_date','end_date','locations','budget','land_page','keywords','CPM','CPC','description','commission','pay_off','ads','advertiser',)

    def get_validation_exclusions(self, *args, **kwargs):
        exclusions = super(CampaignSerializer,self).get_validation_exclusions()

        return exclusions + ['advertiser']

    def create(self, validated_data):
        ads_data = validated_data.pop('ads')
        campaign = Campaign.objects.create(**validated_data)
        for ad_data in ads_data:
            ADS.objects.create(campaign=campaign, **ad_data)
        return campaign

这不起作用,无论我在哪里看到这个答案,都不知道为什么?

  

OrderedDict([(' name',u' Demo Campaign'),(u' type',u' PayPerSale'),(u&# 39;地点',你'马其顿'),(u'预算',10),(你' land_page',你' http://www.test.com& #39;),(u'关键字',u' k1,k2,k3,k4'),(u' CPM',1),(u' CPC& #39;,2),(u'说明',u'演示说明'),(u'佣金',10),(u' pay_off' ,20),(u' ads',[OrderedDict([('标题',u'演示标题'),(u'广告客户') ])])])

0 个答案:

没有答案