1
我有一個模型:如何將字段從json映射到drf serialiazer中的模型字段?
class Book(models.Model):
genre_type_id = models.ForeignKey(GenreType)
author = models.ForeignKey(Author)
title = models.CharField(max_length=255)
year = models.DateField()
和串行:
class BookSerializer(ModelSerializer):
model = Book
fields = ('id', 'genre_type_id', 'author', 'year')
read_only_fields = ('id',)
服務器接收POST請求數據:
{
"genre": 1,
"author": 3,
"title": "My title",
"year": "2016.01.01"
}
如何genre
場從JSON映射到genre_type_id
drf序列化器中的模型字段?
我想我可以這樣做: 'mapping_fields =(('gender','genre_type_id'),'' –