[英]How can you show more than just the foreign key of an object when listing something in Django Rest Framework?
I'm working on a problem in one class called Tour, which includes an Artist foreign key. 我正在一个名为Tour的课程中研究一个问题,其中包括Artist外键。 I need to show more than just its foreign key.
我需要展示的不仅仅是它的外键。 How can I do that while keeping all of CRUD functional?
在保持所有CRUD功能正常的情况下,我该怎么做?
I tried to add depth = 1 to the Meta class in the serializers. 我试图在序列化器中将depth = 1添加到Meta类。 That works, but upon attempting to create a new object, I get the error NOT_NULL CONSTRAINT FAILED.
可以,但是在尝试创建新对象时,出现错误NOT_NULL CONSTRAINT FAILED。 Then I got rid of that and instead added artist = ArtistSerializer to the TourSerializer, but that still doesn't do it, even though I've seen it work in a tutorial.
然后我摆脱了这个问题,而是将artist = ArtistSerializer添加到TourSerializer中,但是即使我在教程中看到了它,它仍然没有实现。 The issue with using artist=ArtistSerializer() is that when I try to create the object, I pass the foreign key, but it doesn't seem to recognize it as a valid field.
使用artist = ArtistSerializer()的问题是,当我尝试创建对象时,我传递了外键,但似乎无法将其识别为有效字段。 It's like I didn't type anything at all into the field.
就像我根本没有在该字段中键入任何内容一样。 Every time I get it to properly list it, the creation doesn't work as intended.
每次我正确列出它时,创建的内容都无法按预期工作。 Here's how my code looks right now:
这是我的代码现在的外观:
class TourSerializer(ModelSerializer):
artist = ArtistSerializer()
class Meta:
model = Tour
fields = ['artist', 'start', 'end', 'country', 'id', 'created_at', 'updated_at']
read_only_fields = ['id', 'created_at', 'updated_at']
The models for Tour and Artist are as follows: Tour和Artist的模型如下:
class Tour(models.Model):
artist = models.ForeignKey('core.Artist', on_delete=models.PROTECT)
start = models.DateField(auto_now=False, auto_now_add=False)
end = models.DateField(auto_now=False, auto_now_add=False)
country = models.CharField(max_length=70)
created_at = models.DateField(auto_now_add=True)
updated_at = models.DateField(auto_now=True)
class Artist(models.Model):
user = models.ForeignKey('core.User', on_delete=models.PROTECT)
name_alternative = models.CharField(max_length=50)
country = models.CharField(max_length=50)
bio = models.TextField(max_length=2500)
profile_image = models.ImageField(upload_to='user/profile', null=True, blank=True)
tags = models.ManyToManyField('core.Tag', blank=True)
labels = models.ManyToManyField('core.Label', blank=True)
created_at = models.DateField(auto_now_add=True)
updated_at = models.DateField(auto_now=True)
And just for good measure, Here's the artist serializer: 出于很好的考虑,这是艺术家序列化器:
class ArtistSerializer(ModelSerializer):
user = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)
class Meta:
model = Artist
fields = ['id', 'user', 'name_alternative', 'labels', 'tags',
'country', 'bio', 'profile_image', 'created_at', 'updated_at']
read_only_fields = ['id', 'created_at', 'updated_at', 'user']
This is my first question here, so let me know if I missed any vital piece of code. 这是我的第一个问题,所以让我知道是否错过任何重要的代码。
Probably you can update the TourSerializer
's to_represent()
method. 可能您可以更新
TourSerializer
的to_represent()
方法。 For example: 例如:
class TourSerializer(ModelSerializer):
class Meta:
model = Tour
fields = ['artist', 'start', 'end', 'country', 'id', 'created_at', 'updated_at']
read_only_fields = ['id', 'created_at', 'updated_at']
def to_representation(self, instance):
response = super().to_representation(instance)
response['artist'] = ArtistSerializer(instance.artist).data
return response
In that way, when you are creating/updating the Tour
instance, you can pass the FK value, but when reading, you will get all the fields from Artist
object. 这样,在创建/更新
Tour
实例时,可以传递FK值,但是在读取时,您将从Artist
对象中获取所有字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.