serializers.py 663 B

12345678910111213141516171819202122232425262728
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. from rest_framework.serializers import CharField, ModelSerializer
  3. from tests.dj_master.models import Author, Book, Publisher
  4. class BookSerializer(ModelSerializer):
  5. name = CharField(source='title')
  6. class Meta:
  7. model = Book
  8. fields = ('id', 'name')
  9. class PublisherSerializer(ModelSerializer):
  10. class Meta:
  11. model = Publisher
  12. fields = '__all__'
  13. class AuthorSerializer(ModelSerializer):
  14. books = BookSerializer(many=True)
  15. publisher = PublisherSerializer()
  16. class Meta:
  17. model = Author
  18. fields = ('id', 'name', 'publisher', 'books')