Django Rest框架:获取所有与之相关的数据 [英] Django Rest Framework: get all data in relation

查看:123
本文介绍了Django Rest框架:获取所有与之相关的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, I have a Django Application and I try to get all information in JSON format or QuerySet format:

models.py / p>

models.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)

在独立脚本中,我想获取所有数据,所以我使用:

In a standalone script I want to get all data, so I used:

seg_ser = serializers.serialize('json', Segment.objects.all())

这是结果:

[
  {
    "model": "dashboard_tibco.segment",
    "pk": 3,
    "fields": {
      "index_number": 1,
      "chain_batch_fueled": "",
      "comment": "",
      "development": 10
   }
  },
]

正如你所看到的,只有数据的信息在这里,而不是开发,流程和应用的信息离子...

As you can see, only informations of data are here, but not the informations of development, flow and application...

任何解决方案来获取开发对象的所有字段,并且与流对象和应用程序对象相同?

Any solution to get all fields of development object and the same for flow object and application object?

解决方案:
感谢Jamie的帮助!

Solution: Thanks Jamie for your help!

所以,这是我的 serializers.py: / strong>

So, this is my serializers.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__'

我的 view.py:

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'))

我的 urls.py:

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'),
]

我的独立脚本与django rest序列化程序:

My standalone script with django rest serializers:

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的结果

[
  {
    "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框架。

I'm going to assume you're using the Django Rest Framework.

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

You need to create a custom serializer. I have not tested it, but it would be something like this:

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)。

This tells the SegementSerializer to use the DevelopmentSerializer when serializing the development data. By default it uses a PrimaryKeyRelatedField which is why you just see the ID (in this case, 10).

这篇关于Django Rest框架:获取所有与之相关的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆