如何使用序列化器获取django中的所有related_name字段?

时间:2018-09-23 14:58:57

标签: python django

这是我的模型。py

from __future__ import unicode_literals
from django.db import models

class User(models.Model):
    name = models.CharField(max_length=200)
    company_name = models.ForeignKey('Company',on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Company(models.Model):
    name = models.CharField(max_length=200)
    phone_number = models.IntegerField(null=True,blank=True)

    def __str__(self):
        return self.name

class Catalog(models.Model):
    name = models.CharField(max_length=200)
    no_of_pcs = models.IntegerField(null=True,blank=True)
    per_piece_price = models.DecimalField(null=True,blank=True,max_digits=10,decimal_places=2)
    company_name = models.ForeignKey(Company,on_delete=models.CASCADE)

    def __str__(self):
        return self.name

这是我的seralizers.py

from rest_framework import serializers
from .models import *
from django.db.models import Sum,Avg,Max,Min,Count,F,Q

class CatalogSerializer(serializers.HyperlinkedModelSerializer):
    dynamic_data = serializers.SerializerMethodField()
    class Meta:
        model = Catalog
        fields = '__all__'

    def get_dynamic_data(self, obj):
        totalpieces = Catalog.objects.all().aggregate(total_pieces=Count('no_of_pcs'))
        totalprice = Catalog.objects.all().aggregate(total_price=Sum('per_piece_price'))
        return totalprice,totalpieces

class CompanySerializer(serializers.ModelSerializer):
    class Meta:
        model = Company
        fields = ('name', 'phone_number', 'catalog','user')

class UserSerializer(serializers.ModelSerializer):
    name = serializers.StringRelatedField()
    company_name = serializers.StringRelatedField()
    class Meta:
        model = User
        fields = '__all__'

这是我的view.py

from __future__ import unicode_literals
from django.http import HttpResponse
from .models import *
import json
from django.http import JsonResponse, HttpResponse

from .serializers import *
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets


class CatalogView(viewsets.ModelViewSet):
    queryset =  Catalog.objects.select_related('company_name')
    serializer_class = CatalogSerializer

class CompanyView(viewsets.ModelViewSet):
    queryset =  Company.objects.all()
    serializer_class = CompanySerializer

class UserView(viewsets.ModelViewSet):
    queryset =  User.objects.all()
    serializer_class = UserSerializer

这是我的urls.py

from django.conf.urls import url, include
from django.contrib import admin

from api import views
from rest_framework.urlpatterns import format_suffix_patterns
from rest_framework import routers

router = routers.DefaultRouter()
router.register('catalogs',views.CatalogView)
router.register('companies',views.CompanyView)
router.register('users',views.UserView)

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', include(router.urls)),
]

我什么时候去  http://127.0.0.1:8000/companies/

越来越

     [
        {
            "name": "google",
            "phone_number": 12,
            "catalog": [
                5
            ]
        }
    ]

我期望如此。

     [
        {
            "url": "google",
            "name": "google",
            "phone_number": 123214214,
            "catalog_details":[
                "name": "sobhagya",
                "no_of_pcs": 22,
                "per_piece_price": "3567.00",
            ]
        }
    ]

在这里,我只能获得related_name的ID(我将其设置为foreignkKey),但是我希望上面的所有字段都是这样。 请检查json api格式

谢谢

1 个答案:

答案 0 :(得分:0)

class CompanySerializer(serializers.ModelSerializer):
    catalog = CatalogSerializer(many=True) # if you want many catalog objects connected to the company else many=False
    class Meta:
        model = Company
        fields = ('name', 'phone_number', 'catalog','user')

您需要在公司序列化程序内部调用目录序列化程序以获取相关对象