SHARE
TWEET

Untitled

a guest May 22nd, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PageSerializer(serializers.ModelSerializer):
  2.     class Meta:
  3.         model = Page
  4.         fields = '__all__'
  5.  
  6.  
  7. class TreeBooksSerializer(serializers.ModelSerializer):
  8.  
  9.     pages = serializers.SerializerMethodField()
  10.  
  11.     def get_pages(self, obj):
  12.         qs = self.context.get('qs')
  13.         for book in qs.books.all():
  14.             if book.id == obj.id:
  15.                 pages = book.pages.all()
  16.         return PageSerializer(pages, many=True).data
  17.  
  18.     class Meta:
  19.         model = Book
  20.         fields = '__all__'
  21.  
  22.  
  23. class TreeSerializer(serializers.ModelSerializer):
  24.  
  25.     books = serializers.SerializerMethodField()
  26.  
  27.     def get_books(self, obj):
  28.         qs = Publisher.objects.filter(id=obj.id).prefetch_related('books', 'books__pages').first()
  29.         books = [book for book in qs.books.all()]
  30.         return TreeBooksSerializer(books, many=True, context={'qs': qs}).data
  31.  
  32.     class Meta:
  33.         model = Publisher
  34.         fields = '__all__'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top