Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Django REST访问完整对象JSON图?_Django_Hibernate_Django Models_Orm_Django Rest Framework - Fatal编程技术网

如何从Django REST访问完整对象JSON图?

如何从Django REST访问完整对象JSON图?,django,hibernate,django-models,orm,django-rest-framework,Django,Hibernate,Django Models,Orm,Django Rest Framework,我有Django模型、序列化程序和视图: from django.db import models # Create your models here. class Contract(models.Model): contract_no = models.IntegerField(blank=False, unique=True) customer = models.IntegerField(blank=False) agent = models.IntegerFiel

我有Django模型、序列化程序和视图:

from django.db import models

# Create your models here.
class Contract(models.Model):
    contract_no = models.IntegerField(blank=False, unique=True)
    customer = models.IntegerField(blank=False)
    agent = models.IntegerField(blank=False)
    agency = models.IntegerField(blank=False)
    cont_date = models.DateField()

    def __str__(self):
        """Return a human readable representation of the model instance."""
        return "{}".format(self.contract_no)

class ContractOrder(models.Model):
    order_no = models.IntegerField(blank=False, unique=True)
    order_date = models.DateField()
    contract = models.ForeignKey(Contract, on_delete=models.DO_NOTHING)

    def __str__(self):
        return self.order_no

    class Meta:
        ordering = ('order_no',)


from rest_framework import serializers
from .models import Contract
from .models import ContractOrder

class ContractSerializer(serializers.ModelSerializer):
    """Serializer to map the Model instance into JSON format."""

    class Meta:
        """Meta class to map serializer's fields with the model fields."""
        model = Contract
        fields = ('contract_no', 'customer', 'agent', 'agency', 'cont_date')
        #read_only_fields = ('date_created', 'date_modified')

class ContractOrderSerializer(serializers.ModelSerializer):
    """Serializer to map the Model instance into JSON format."""

    class Meta:
        """Meta class to map serializer's fields with the model fields."""
        model = ContractOrder
        fields = ('order_no', 'order_date', 'contract')
        #read_only_fields = ('date_created', 'date_modified')


from rest_framework import generics
from .serializers import ContractSerializer
from .models import Contract
from .serializers import ContractOrderSerializer
from .models import ContractOrder

class ContractCreateView(generics.ListCreateAPIView):
    """This class defines the create behavior of our rest api."""
    queryset = Contract.objects.all()
    serializer_class = ContractSerializer

    def perform_create(self, serializer):
        """Save the post data when creating a new bucketlist."""
        serializer.save()

class ContractOrderCreateView(generics.ListCreateAPIView):
    """This class defines the create behavior of our rest api."""
    queryset = ContractOrder.objects.all()
    serializer_class = ContractOrderSerializer

    def perform_create(self, serializer):
        """Save the post data when creating a new bucketlist."""
        serializer.save()
结果/合同和/合同订单JSON消息很简单:

[{"contract_no":1,"customer":1,"agent":1,"agency":1,"cont_date":"2018-04-26"},{"contract_no":2,"customer":2,"agent":2,"agency":2,"cont_date":"2018-04-25"}]

[{"order_no":1,"order_date":"2018-04-26","contract":1}]
但我希望有更多扩展的JSON消息,特别是:

  • Contract JSON实体应该具有ContractOrder实体的数组(在我的例子中,Contract 1应该具有带有1个条目的数组)

  • ContractOrder JSON实体应该有扩展的合同实体-目前合同字段(JSON)只包含标量值1,但我希望有完整的合同实体

  • 当然,有时候JSON图应该完全展开,有时候应该收缩,有时候应该部分展开?在Django有可能做到这一点吗


    我的经验是,在Java JPA/Hibernate/Spring中,可以使用注释
    @OneToMany
    @JsonView
    (来自
    com.fasterxml.jackson.annotation.JsonView
    库)控制实体图(在内存中)和实体图的JSON视图(在REST中)但是,如何在Django中实现这一点—在内存和REST JSON消息中都是如此?

    请按如下方式更改序列化程序:

    class SimpleContractOrderSerializer(serializers.ModelSerialiser):  # Required; otherwise you get into a recursive loop.
        class Meta:
            """Meta class to map serializer's fields with the model fields."""
            model = ContractOrder
            fields = ('order_no', 'order_date',)
    
    class ContractSerializer(serializers.ModelSerializer):
        """Serializer to map the Model instance into JSON format."""
        contract_order = SimpleContractOrderSerializer(source='contractorder', many=True, read_only=True)
    
        class Meta:
            """Meta class to map serializer's fields with the model fields."""
            model = Contract
            fields = ('contract_no', 'customer', 'agent', 'agency', 'cont_date')
            #read_only_fields = ('date_created', 'date_modified')
    
    class ContractOrderSerializer(serializers.ModelSerializer):
        """Serializer to map the Model instance into JSON format."""
        contract = ContractSerializer(read_only=True)
    
        class Meta:
            """Meta class to map serializer's fields with the model fields."""
            model = ContractOrder
            fields = ('order_no', 'order_date', 'contract')
            #read_only_fields = ('date_created', 'date_modified')
    
    请在此处阅读更多信息: