gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:08:41 25 4
gpt4 key购买 nike

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

输出即时消息:

[
{
"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"
}
]

我想要什么:
我想返回表中的外键 (MainCatName) 值而不是 ID。即我的 models.py 中 [CategoryName = models.CharField(max_length=50)] 中的值

喜欢

[
{
"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

模型.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)+' $'

序列化程序.py :

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

最佳答案

你有 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 数据库查询。

关于django - 在 django rest 框架中,如何获取外键的值而不是 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43072340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com