如何在django rest框架中获取外键的值而不是id

时间:2017-03-28 14:31:20

标签: django django-rest-framework

抱歉,我的英语不好,希望你明白我的意思。

OUT PUT IM GETTING:

[
    {
        "MainCatName": 1,
        "Name": "harry potter",
        "Image": "/media/101029496--sites-default-files-images-101029496-3176173-1748009911-hp.jp-1_MoxqrLp.jpg"
    },
    {
        "MainCatName": 2,
        "Name": "Princes Girl",
        "Image": "/media/character_princess_rapunzel_8320d57a.jpeg"
    },
    {
        "MainCatName": 3,
        "Name": "sex in the city",
        "Image": "/media/250px-SATC_Title.jpg"
    },
    {
        "MainCatName": 4,
        "Name": "who is dragon",
        "Image": "/media/Reggio_calabria_museo_nazionale_mosaico_da_kaulon.jpg"
    },
    {
        "MainCatName": 2,
        "Name": "drama queen",
        "Image": "/media/15241421_170761763390015_7913498865987146084_n.jpg"
    }
]

我想要什么:
我想要返回ID为表的INIGN键中的FORIGN KEY(MainCatName)值。即我的models.py中的[CategoryName = models.CharField(max_length = 50)]中的值

LIKE

[
    {
        "MainCatName": Story Books,
        "Name": "harry potter",
        "Image": "/media/101029496--sites-default-files-images-101029496-3176173-1748009911-hp.jp-1_MoxqrLp.jpg"
    },
    {
        "MainCatName": Darama,
        "Name": "Princes Girl",
        "Image": "/media/character_princess_rapunzel_8320d57a.jpeg"
    },
    {
        "MainCatName": Roamance,
        "Name": "sex in the city",
        "Image": "/media/250px-SATC_Title.jpg"
    },
    {
        "MainCatName": sex,
        "Name": "who is dragon",
        "Image": "/media/Reggio_calabria_museo_nazionale_mosaico_da_kaulon.jpg"
    },
    {
        "MainCatName": darama,
        "Name": "drama queen",
        "Image": "/media/15241421_170761763390015_7913498865987146084_n.jpg"
    }
]

这是我的代码:

veiws.py:

class GETCATVeiw(APIView):
    def get(self, request):
        data = Products.objects.only('MainCatName','Name','Image')
        serializer = GETCAT(data, many=True)
        return Response(serializer.data)

    def post(self):
        pass

models.py:

class Category(models.Model):
    CategoryName = models.CharField(max_length=50)
    class Meta:
            verbose_name_plural = 'Categories'

        def __str__(self):
        return self.CategoryName

class Products(models.Model):
    MainCatName = models.ForeignKey(Category, on_delete=models.CASCADE)
    Name = models.CharField(max_length=50)
    Image = models.ImageField()
    Price = models.IntegerField()
    DiscriptionHeading = models.CharField(max_length=100)
    DiscriptionParagraph = models.TextField(max_length=1000)

    class Meta:
            verbose_name_plural = 'Products'

        def __str__(self):
            return str(self.MainCatName)+' - '+self.Name+' - '+str(self.Price)+' $'

serializers.py:

class GETCAT(serializers.ModelSerializer):
    class Meta:
        model = Products
        fields = ('MainCatName', 'Name', 'Image')

2 个答案:

答案 0 :(得分:4)

你有SlugRelatedField

class GETCAT(serializers.ModelSerializer):
    MainCatName = serializers.SlugRelatedField(read_only=True, slug_field='CategoryName')
    class Meta:
        model = Products
        fields = ('MainCatName', 'Name', 'Image')

编辑: 我不确定它是否可以与Products.objects.only('MainCatName','Name','Image')一起使用,因为您将跨越数据库关系。此外,您可能希望使用select_related来避免获得N + 1数据库查询。

答案 1 :(得分:0)

通用的Django方法是在模型上定义natural keys,在本例中为Category

  1. 按自然键进行序列化:在natural_key(self)类中添加Category方法,在该类中返回类别CategoryName。它必须是独一无二的!

    def natural_key(self):
        return self.CategoryName
    
  2. 使用自然键进行反序列化:您希望为类别模型定义默认管理器:

    objects = CategoryManager()
    

    并在get_by_natural_key(self, name)类中定义CategoryManager(models.Manager)方法,该方法返回类别:

    class CategoryManager(models.Manager):
    
        def get_by_natural_key(self, name):
            return self.get(CategoryName=name)