Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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
Python Django Rest框架:获取相关的所有数据_Python_Django_Django Rest Framework - Fatal编程技术网

Python Django Rest框架:获取相关的所有数据

Python Django Rest框架:获取相关的所有数据,python,django,django-rest-framework,Python,Django,Django Rest Framework,因此,我有一个Django应用程序,我尝试以JSON格式或QuerySet格式获取所有信息: 型号.py class Flow(models.Model): name = models.CharField("nom", primary_key=True, max_length=255) BL_applicative = models.CharField("BL applicative", max_length=255,blank=True, null=True) comm

因此,我有一个Django应用程序,我尝试以JSON格式或QuerySet格式获取所有信息:

型号.py

class Flow(models.Model):
    name = models.CharField("nom", primary_key=True, max_length=255)
    BL_applicative = models.CharField("BL applicative", max_length=255,blank=True, null=True)
    comment = models.TextField("commentaire", max_length=1500,blank=True,null=True)
    application = models.ForeignKey('Application', null=True)

class Development(models.Model):
    stability = models.IntegerField("stabilité", default=0)
    unit_test = models.IntegerField("tests unitaires", default=0)
    documentation = models.IntegerField(default=0)
    conception = models.IntegerField(default=0)
    production = models.IntegerField("réalisation", default=0)
    flow = models.ForeignKey('Flow',blank=True,null=True)

class Segment(models.Model):
    index_number = models.IntegerField("indice")
    chain_batch_fueled = models.CharField(max_length=255, blank=True,null=True)
    comment = models.TextField("commentaire", max_length=1500, blank=True,null=True)
    development = models.ForeignKey('Development',verbose_name="Développement", blank=True,null=True)
在独立脚本中,我希望获取所有数据,因此我使用:

seg_ser = serializers.serialize('json', Segment.objects.all())
这就是结果:

[
  {
    "model": "dashboard_tibco.segment",
    "pk": 3,
    "fields": {
      "index_number": 1,
      "chain_batch_fueled": "",
      "comment": "",
      "development": 10
   }
  },
]
如你所见,这里只有数据信息,而没有开发、流程和应用的信息

是否有任何解决方案可以获得开发对象的所有字段以及流对象和应用程序对象的相同字段

解决方案: 谢谢杰米的帮助

这是我的序列化程序。py:

from rest_framework import serializers

from dashboard_tibco.models import Development, Segment


class DevelopmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Development
        fields = '__all__'


class SegmentSerializer(serializers.ModelSerializer):
    development = DevelopmentSerializer(read_only=True)

    class Meta:
        many = True
        model = Segment
        fields = '__all__'
from django.http import HttpResponse
from dashboard_tibco.transformation_document.document import Document
def get_json_doc(request):
    return HttpResponse(Document().get_sql_data_by_application('GRS'))
from django.conf.urls import url
from django.contrib import admin


from dashboard_tibco.views import get_json_doc

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^json', get_json_doc, name='json'),
]
[
  {
    "id": 3,
    "development": {
        "id": 10,
        "status": "En cours",
        "stability": 0,
        "unit_test": 0,
        "documentation": 0,
        "conception": 0,
        "production": 0,
        "modification_date": null,
        "flow": "Batch",
        "achievement_lot": null,
        "project": null
     },
     "name": "",
     "index_number": 1,
     "pivot_subscribed": "",
     "pivot_published": "",
     "chain_batch_fueled": "",
     "comment": "",
     "called": null,
     "caller": null,
     "tuxedo_adapter": null
  },
]
我的视图。py:

from rest_framework import serializers

from dashboard_tibco.models import Development, Segment


class DevelopmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Development
        fields = '__all__'


class SegmentSerializer(serializers.ModelSerializer):
    development = DevelopmentSerializer(read_only=True)

    class Meta:
        many = True
        model = Segment
        fields = '__all__'
from django.http import HttpResponse
from dashboard_tibco.transformation_document.document import Document
def get_json_doc(request):
    return HttpResponse(Document().get_sql_data_by_application('GRS'))
from django.conf.urls import url
from django.contrib import admin


from dashboard_tibco.views import get_json_doc

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^json', get_json_doc, name='json'),
]
[
  {
    "id": 3,
    "development": {
        "id": 10,
        "status": "En cours",
        "stability": 0,
        "unit_test": 0,
        "documentation": 0,
        "conception": 0,
        "production": 0,
        "modification_date": null,
        "flow": "Batch",
        "achievement_lot": null,
        "project": null
     },
     "name": "",
     "index_number": 1,
     "pivot_subscribed": "",
     "pivot_published": "",
     "chain_batch_fueled": "",
     "comment": "",
     "called": null,
     "caller": null,
     "tuxedo_adapter": null
  },
]
MyURL.py:

from rest_framework import serializers

from dashboard_tibco.models import Development, Segment


class DevelopmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Development
        fields = '__all__'


class SegmentSerializer(serializers.ModelSerializer):
    development = DevelopmentSerializer(read_only=True)

    class Meta:
        many = True
        model = Segment
        fields = '__all__'
from django.http import HttpResponse
from dashboard_tibco.transformation_document.document import Document
def get_json_doc(request):
    return HttpResponse(Document().get_sql_data_by_application('GRS'))
from django.conf.urls import url
from django.contrib import admin


from dashboard_tibco.views import get_json_doc

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^json', get_json_doc, name='json'),
]
[
  {
    "id": 3,
    "development": {
        "id": 10,
        "status": "En cours",
        "stability": 0,
        "unit_test": 0,
        "documentation": 0,
        "conception": 0,
        "production": 0,
        "modification_date": null,
        "flow": "Batch",
        "achievement_lot": null,
        "project": null
     },
     "name": "",
     "index_number": 1,
     "pivot_subscribed": "",
     "pivot_published": "",
     "chain_batch_fueled": "",
     "comment": "",
     "called": null,
     "caller": null,
     "tuxedo_adapter": null
  },
]
带有django rest序列化程序的独立脚本:

class Document(object):
    def __init__(self):
        /..Make something../

    def get_sql_data_by_application(self):
        serializer = SegmentSerializer(Segment.objects.all(), many=True)
        return JSONRenderer().render(serializer.data)
以及JSONRenderer:

from rest_framework import serializers

from dashboard_tibco.models import Development, Segment


class DevelopmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Development
        fields = '__all__'


class SegmentSerializer(serializers.ModelSerializer):
    development = DevelopmentSerializer(read_only=True)

    class Meta:
        many = True
        model = Segment
        fields = '__all__'
from django.http import HttpResponse
from dashboard_tibco.transformation_document.document import Document
def get_json_doc(request):
    return HttpResponse(Document().get_sql_data_by_application('GRS'))
from django.conf.urls import url
from django.contrib import admin


from dashboard_tibco.views import get_json_doc

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^json', get_json_doc, name='json'),
]
[
  {
    "id": 3,
    "development": {
        "id": 10,
        "status": "En cours",
        "stability": 0,
        "unit_test": 0,
        "documentation": 0,
        "conception": 0,
        "production": 0,
        "modification_date": null,
        "flow": "Batch",
        "achievement_lot": null,
        "project": null
     },
     "name": "",
     "index_number": 1,
     "pivot_subscribed": "",
     "pivot_published": "",
     "chain_batch_fueled": "",
     "comment": "",
     "called": null,
     "caller": null,
     "tuxedo_adapter": null
  },
]

我假设您使用的是Django Rest框架

您需要创建一个自定义序列化程序。我没有测试过它,但它会是这样的:

class DevelopmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Development

class SegmentSerializer(serializers.ModelSerializer):
    development = DevelopmentSerializer(read_only=True)
    class Meta:
        many = True
        model = Segment

这告诉
SegementSerializer
在序列化
开发
数据时使用
DevelopmentSerializer
。默认情况下,它使用一个
PrimaryKeyRelatedField
,这就是为什么您只看到ID(在本例中为10)。

我假设您使用的是Django Rest框架

您需要创建一个自定义序列化程序。我没有测试过它,但它会是这样的:

class DevelopmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Development

class SegmentSerializer(serializers.ModelSerializer):
    development = DevelopmentSerializer(read_only=True)
    class Meta:
        many = True
        model = Segment

这告诉
SegementSerializer
在序列化
开发
数据时使用
DevelopmentSerializer
。默认情况下,它使用一个
PrimaryKeyRelated字段
,这就是为什么您只看到ID(在本例中为10)。

谢谢帮助,我将尝试此解决方案。谢谢帮助,我将尝试此解决方案。