使用ForeignKey字段获取自定义序列化模型的最简单方法是什么? [英] What is the easiest way to get custom serialized model with ForeignKey field
问题描述
#models.py
class作者(models.Model):
name = models.CharField(max_length = 255)
class Book(models.Model):
book_written_by = models.ForeignKey(作者)
class Song model.Model):
song_written_by = models.ForeignKey(作者)
#serializers.py
class CustomSerializer(serializers.ModelSerializer):
#在这里做什么? #
class BookSerializer(CustomSerializer):
class Meta:
model = Book
class SongSerializer(CustomSerializer):
class Meta:
model = Song
我想得到以下结果:
kleist = Author.objects.create(name ='Kleist')
some_book = Book.objects.create(author = kleist)
BookSerializer(some_book).data == {'id':1,'book_written_by':{'作者':1}}#返回True
我试图覆盖get_related_field()并将PrimaryKeyRelatedField更改为CustomPrimaryKeyRelatedField,并尝试覆盖to_native(),但是我无法访问模型字段及其对象本身,
你需要的是一定可以实现的,但这真的是打破惯例,所以我个人投票反对这样做。然而,实现所需要的方法是使用 SerializerMethodField
:
class BookSerializer(serializers.ModelSerializer):
book_written_by = serializers.SerializerMethodField('get_author')
class Meta:
model =用户
def get_author(self,obj):
return {'作者':obj.book_written_by.pk}
这将返回您想要的确切输出。如果你决定遵守这些惯例,我更喜欢这样的输出:
{'id':1,' book_written_by':{'id':1,'name':'somebody'}}
以下serializer将返回给你:
class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields =('id','name')
class BookSerializer(serializers.ModelSerializer):
book_written_by = AuthorSerializer()
I am looking for an easy way to subclass rest_framework.serializers.ModelSerializer from Django REST framework so that it serializes to a special dictionary for foreign key models fields with the name of the model class and the id value instead of just an integer / primary key. This should work for different models.
# models.py
class Author(models.Model):
name = models.CharField(max_length=255)
class Book(models.Model):
book_written_by = models.ForeignKey(Author)
class Song(models.Model):
song_written_by = models.ForeignKey(Author)
# serializers.py
class CustomSerializer(serializers.ModelSerializer):
# What to do here? #
class BookSerializer(CustomSerializer):
class Meta:
model = Book
class SongSerializer(CustomSerializer):
class Meta:
model = Song
I would like to get the following result:
kleist = Author.objects.create(name='Kleist')
some_book = Book.objects.create(author=kleist)
BookSerializer(some_book).data == {'id': 1, 'book_written_by': {'Author': 1}} # Returns True
I tried to override get_related_field() and changed PrimaryKeyRelatedField to a CustomPrimaryKeyRelatedField and there I tried to override to_native() but I don't have access to the model field and its object itself but only to the pk value.
What you need is certainly achievable, but it is really breaking convention so personally I'd vote against doing so. Nevertheless, a way to achieve what you need is to use a SerializerMethodField
:
class BookSerializer(serializers.ModelSerializer):
book_written_by = serializers.SerializerMethodField('get_author')
class Meta:
model = User
def get_author(self, obj):
return {'Author': obj.book_written_by.pk}
This would return the exact output you wanted. If you decide to stick with the conventions, I'd prefer an output like this:
{'id': 1, 'book_written_by': {'id': 1, 'name': 'somebody'}}
The following serializer would return that for you:
class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields = ('id', 'name')
class BookSerializer(serializers.ModelSerializer):
book_written_by = AuthorSerializer()
这篇关于使用ForeignKey字段获取自定义序列化模型的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!