如何在Django Rest中显示外键的值

时间:2016-10-08 02:21:09

标签: python django django-rest-framework

我希望我的输出json显示外键指向的记录的值而不是键。

例如,我想让它显示出来:

[
    {
        "id": 1,
        "brand": "ATL Motors",
        "package": "Full Page",
        "newspaper": "Gleaner",
        "cost": 22000,
        "objective": "Brand Awareness",
        "ad_date": "2016-10-01",
        "created_on": "2016-10-07T20:21:52Z"
    }

而不是:

[
    {
        "id": 1,
        "brand": 2,
        "package": "Full Page",
        "newspaper": 1,
        "cost": 22000,
        "objective": "Brand Awareness",
        "ad_date": "2016-10-01",
        "created_on": "2016-10-07T20:21:52Z"
    }
]

这是我的models.py文件:

from django.db import models
from django.utils import timezone


class Brand(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    website = models.URLField()
    created_on = models.DateTimeField(default=timezone.now())

    def __str__(self):
        return self.name


class Newspaper(models.Model):
    name = models.CharField(max_length=200)
    created_on = models.DateTimeField(default=timezone.now())

    def __str__(self):
        return self.name


class PurchasedAd(models.Model):
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
    package = models.CharField(max_length=350)
    newspaper = models.ForeignKey(Newspaper, on_delete=models.CASCADE)
    cost = models.IntegerField()
    objective = models.TextField()
    ad_date = models.DateField()
    created_on = models.DateTimeField(default=timezone.now())

    def __str__(self):
        string = "{} - {}".format(self.brand, self.objective)
        return string

这是我的serializer.py文件:

from rest_framework import serializers
from . import models


class BrandSerializer(serializers.ModelSerializer):
    class Meta:
        fields = (
            'id',
            'name',
            'description',
            'website',
            'created_on',
        )
        model = models.Brand


class PurchasedAdSerializer(serializers.ModelSerializer):
    class Meta:
        fields = (
            'id',
            'brand',
            'package',
            'newspaper',
            'cost',
            'objective',
            'ad_date',
            'created_on',
        )
        model = models.PurchasedAd

这是我的views.py文件:

from rest_framework import generics
from . import models
from . import serializers


class ListBrand(generics.ListAPIView):
    queryset = models.Brand.objects.all()
    serializer_class = serializers.BrandSerializer


class ListAds(generics.ListAPIView):
    queryset = models.PurchasedAd.objects.all()
    serializer_class = serializers.PurchasedAdSerializer

请帮助!!

1 个答案:

答案 0 :(得分:0)

您可以使用StringRelatedField

  

StringRelatedField可用于表示目标   关系使用 unicode 方法。

class PurchasedAdSerializer(serializers.ModelSerializer):
    newspaper = serializers.StringRelatedField()

    class Meta:
        fields = (
            'id',
            'brand',
            'package',
            'newspaper',
            'cost',
            'objective',
            'ad_date',
            'created_on',
        )
        model = models.PurchasedAd