0

コードで次の応答があります。

{
    "id": 716,
    "name": "XYZ",
    "start_date": "2019-12-24",
    "end_date": "2020-01-31",        
    "ads": [
        {
            "id": 20228,
            "no_of_times_per_hr": 1,
        },
        {
            "id": 20227,
            "no_of_times_per_hr": 2
        },
        {
            "id": 20229,
            "no_of_times_per_hr": 7                
        }
    ]
},

次のコードでシリアライザーを使用してこれを作成しました。

class AdsDetailOnScheduler(serializers.ModelSerializer):
    ads = serializers.SerializerMethodField()
    class Meta:
      model = AdCampaign
      fields = ('id','name','start_date','end_date', 'ads')

    def get_ads(self, obj):
      adlabels = AdDayLabelMap.objects.filter(ad_campaign__id  = obj['id']).values_list('ad_label', flat = True).distinct()
      ad_id = AdSlot.objects.filter(ad_label__id__in = adlabels).values_list('ads', flat = True)
      if Ads.objects.filter(id__in = ad_id).exists():
        a = []
        for ad in Ads.objects.filter(id__in = ad_id):
            a.append(AdsDetailSongScheduler(ad).data)
        return a
      else:
        return None  

class AdsDetailSongScheduler(serializers.ModelSerializer):  
    no_of_times_per_hr = serializers.SerializerMethodField()
    after_n_songs = serializers.SerializerMethodField()
    specific_time = serializers.SerializerMethodField()
    class Meta:
      model = Ads
      fields = ('id','no_of_times_per_hr')

    def get_no_of_times_per_hr(self, obj):          
      if obj.no_of_times_per_hr:
         return obj.no_of_times_per_hr
      else:
         return None

ただし、以下の応答が必要です。

{
    "id": 716,
    "name": "XYZ",
    "start_date": "2019-12-24",
    "end_date": "2020-01-31",        
    "ads": [
        {
            "id": 20228,
            "no_of_times_per_hr": 1,
        },
    ]
},



{
    "id": 716,
    "name": "XYZ",
    "start_date": "2019-12-24",
    "end_date": "2020-01-31",        
    "ads": [
        {
            "id": 20227,
            "no_of_times_per_hr": 2
        },
    ]
},



{
    "id": 716,
    "name": "XYZ",
    "start_date": "2019-12-24",
    "end_date": "2020-01-31",        
    "ads": [
        {
            "id": 20229,
            "no_of_times_per_hr": 7                
        }
    ]
},

1 回の応答で広告がリストとして表示されることを説明したいと思います。name、start_date、end_date の値が同じである広告を中断したかったのです。そうすることは可能ですか?

更新 1:

その後、私が得る応答の1つ、

[
    {
        "end_date": "2020-01-31",
        "id": 76,
        "ads": [
            {
                "id": 208,                    
                "no_of_times_per_hr": 1
            }
        ],
        "name": "XYZ",
        "start_date": "2019-12-24"
    },
    {
        "end_date": "2020-01-31",
        "id": 76,
        "ads": [
            {
                "id": 207,                
                "no_of_times_per_hr": 2
            }
        ],
        "name": "XYZ",            
        "start_date": "2019-12-24"
    },
    {
        "end_date": "2020-01-31",
        "id": 76,
        "ads": [
            {
                "id": 209,                    
                "no_of_times_per_hr": 7
            }
        ],
        "name": "XYZ",
        "start_date": "2019-12-24"
    }
],

上記のレスポンスは、3 つではなく 1 つの JSON レスポンスとしてカウントされます。これは、レスポンスが 3 つの異なるリストではなく 1 つのリストであるためです。それで、誰かがこれを手伝ってくれたら?

4

1 に答える 1