用新字典更新序列化器

时间:2018-09-14 07:38:21

标签: django

我创建了一个简单的服务,该服务根据URL中的参数从单个表返回数据,如下所示: 我想将另一个对象附加到JSON列表中:

{
"execution_time": "13:00"
}

因此结果可以是:

[
    {
    "figonr": "1078924300-01",
    "description": "F150/C+W/CC/GE201S/S/OE/SE",
    "productgroup": null
    },
    {
    "execution_time": "13:00"
    }
]

如何将以上对象添加到JSON对象列表中? 谢谢

http://127.0.0.1:8000/api/figo?figonr=1078924300-01
output is the json:
HTTP 200 OK
Allow: GET
Content-Type: application/json
Vary: Accept

[
   {
        "figonr": "1078924300-01",
        "description": "F150/C+W/CC/GE201S/S/OE/SE",
        "productgroup": null
   }
]

我的观点如下:

from django.shortcuts import render
from django.http import HttpResponse

 # Create your views here.

 from rest_framework import generics
 from servicesapp.models import Figo
 from servicesapp.serializers import ServiceAppSerializer


  class ListCreateFigos(generics.ListCreateAPIView):
        serializer_class = ServiceAppSerializer

        http_method_names = ['get']

        def get_queryset(self):
             queryset = None
             figo_nr = self.request.query_params.get('figonr', None)
             if figo_nr is not None:
                   queryset = Figo.objects.filter(figonr = figo_nr)
             return queryset

和序列化器:

from rest_framework import serializers
from servicesapp.models import Figo
import datetime

class ServiceAppSerializer(serializers.ModelSerializer):
     class Meta:
          model = Figo
          fields=('__all__')

0 个答案:

没有答案