如何在django中为json响应

时间:2017-09-19 02:12:54

标签: python django django-models django-rest-framework django-serializer

我是Django的新手,所以我想创建一个代表运输公司运营的基本应用程序。我有一个包含货件的WorkOrder。所以我的models.py包含以下内容:

class WorkOrder (models.Model):
    status = models.CharField(max_length=300,default = "New")
    source = models.CharField(max_length=300)
    destination = models.CharField(max_length=
    material = models.CharField(max_length=300)
    shipmentlist = [] //PROBLEMATIC CODE

class Shipment (models.Model):
    expected_startDate = models.DateTimeField(default=timezone.now)
    expected_endDate = models.DateTimeField(default=timezone.now)
    shipment_status = models.CharField(max_length=300,default = "Not Started")

我有2个串行器WorkOrderSerializer和ShipmentSerializer,我在serialzers.py中定义了它。我想返回工作订单对象中包含的货件清单。

class WorkOrderSerializer        
    generated_date = models.DateTimeField(default=timezone.now)
    status = models.CharField(max_length=300, default="New")
    source = models.CharField(max_length=300)
    destination = models.CharField(max_length=300)
    material = models.CharField(max_length=300)
    shipmentlist = ShipmentSerializer(many=True)

class ShipmentSerializer
    expected_startDate = models.DateTimeField(default=timezone.now)
    expected_endDate = models.DateTimeField(default=timezone.now)
    shipment_status = models.CharField(max_length=300, default="Not Started")

我遵循此处指定的模型。 http://www.django-rest-framework.org/api-guide/serializers/#dealing-with-nested-objects

在我的views.py中,当我收到像这样的请求时,我正在调用序列化程序

def workorder_operations(request,workorder_pk):
    workorder = Work_Order.objects.filter(pk=workorder_pk)
    serializer = Work_Order_Serializer(workorder)

这产生的是一个类似于以下

的json
{
    "shipmentlist":[]
}

我对两件事感到困惑:

  1. 为什么它只显示出货对象而不显示其他对象,即使它无法解析数据或其他内容。
  2. 为什么不装运货物。
  3. 当我使用ModelSerializer代替并以这种方式定义序列化程序时,它完全正常工作:

    class ShipmentSerializer(serializers.ModelSerializer):
        class Meta:
            model = Shipment
            fields =('expected_startDate','expected_endDate','shipment_status')
    class WorkOrderSerializer(serializers.ModelSerializer):
        class Meta:
            model = WorkOrder
            fields =('request_id','generated_date','status', 'source','destination','material')
    

    我想知道的是如何表示嵌套的对象列表,以便可以正确地序列化它们。 我希望我的json for WorkOrder对象看起来像这样:(注意:json和模型中的变量名称可能有不匹配所以请忽略,因为我已经删除了一些变量而不会使这里的示例复杂化。)< / p>

    {
    "id": "WO20170912",
    "source": "BBSR",
    "destination": "RKL",
    "customer_id": 1,
    "material": "Granite",
    "weight": 19,
    "status": "ALLOCATED",
    "shipments": [
      {
        "id":"SH01234",
        "work_order_id": "WO20170912",
        "source": "BBSR",
        "destination": "RKL"
      },
      {
        "id":"SH01255",
        "work_order_id": "WO20170912",
        "source": "BBSR",
        "destination": "RKL"
      }
    ]
    }
    

1 个答案:

答案 0 :(得分:2)

ManyToManyField模型中添加WorkOrder

class Shipment (models.Model):
    expected_startDate = models.DateTimeField(default=timezone.now)
    expected_endDate = models.DateTimeField(default=timezone.now)
    shipment_status = models.CharField(max_length=300,default = "Not Started")


class WorkOrder (models.Model):
    status = models.CharField(max_length=300,default = "New")
    source = models.CharField(max_length=300)
    destination = models.CharField(max_length=
    material = models.CharField(max_length=300)
    shipments = models.ManyToManyField(Shipment, related_name='shipments')

序列化工具就像,

class ShipmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Shipment
        fields = [f.name for f in model._meta.fields]

class WorkOrderSerializer(serializers.ModelSerializer):
    shipments = ShipmentSerializer(many=True)
    class Meta:
        model = WorkOrder
        fields = [f.name for f in model._meta.fields] + ['shipments']