class BookSerializer(serializers.ModelSerializer): authors = AuthorSerializer(many=True) categories = CategorySerializer(many=True) class Meta: model = Books fields = ('id', 'book_name', 'published_date', 'authors', 'categories')
class BookSerializer(serializers.ModelSerializer): authors = AuthorSerializer(many=True) class Meta: model = Book fields = ['id', 'name', 'edition', 'publication_year', 'authors'] def update(self, instance, data): authors_data = data.pop('authors') instance.name = data.get('name', instance.name) instance.edition = data.get('edition', instance.edition) instance.publication_year = data.get('publication_year', instance.publication_year) for author in authors_data: author_queryset = Author.objects.filter( name__exact=author.get('name', None)) if author_queryset.exists(): author = author_queryset.first() else: raise Http404 instance.authors.clear() instance.authors.add(author) instance.save() return instance
class ToDoReadSerializer(ModelSerializer): project = ProjectSerializer(read_only=True) author = AuthorSerializer(read_only=True) class Meta: model = ToDo fields = '__all__'
class BookSerializer(serializers.HyperlinkedModelSerializer): id = HashidSerializerCharField(source_field='books.Book.id') author = AuthorSerializer() #author = serializers.PrimaryKeyRelatedField(pk_field=HashidSerializerCharField(source_field='authors.Author.user'), read_only=True) class Meta: model = Book fields = ('url', 'id', 'name', 'author',)
class PRSerializer(serializers.ModelSerializer): """ General pupose """ author = AuthorSerializer(read_only=True) class Meta: model = PR fields = ('id', 'merge', 'author', 'title', 'description', 'status')
class BookSerializer(serializers.ModelSerializer): book_images = BookImageSerializer(read_only=True, many=True) comment_book = CommentSerializer(read_only=True, many=True) author_book = AuthorSerializer(read_only=True, many=True) class Meta: model = Book fields = ('category', 'book_name', 'author', 'description', 'year_of_issue', 'price', 'rating', 'book', 'book_images', 'comment_book', 'author_book')
class CommitSerializer(serializers.ModelSerializer): """ Geneneral purpose Commit Serializer """ author = AuthorSerializer(read_only=True) branches = BranchSerializer(read_only=True) class Meta: model = Commit fields = ('message', 'author', 'branches', 'files_changed', 'timestamp')
class ListCommitSerializer(serializers.ModelSerializer): """ List Commit Serializer without related fields """ author = AuthorSerializer(read_only=True) branches = BranchSerializer(read_only=True) class Meta: model = Commit fields = ('message', 'author', 'timestamp', 'branches')
def get_authors_view(request, slug): try: author = Author.objects.get(slug=slug) except Author.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == "GET": serializer = AuthorSerializer(author) return Response(serializer.data)
class BookSerializer(serializers.ModelSerializer): author = AuthorSerializer() genre = GenreSerializer(many=True) def create(self, validated_data): book = Book.objects.create( title = validated_data.get('title'), year = validated_data.get('year'), price = validated_data.get('price')) b_auth = validated_data.get('author').get('name') try: auth = Author.objects.get(name=b_auth) except Author.DoesNotExist: auth = Author.objects.create(name=b_auth) book.author = auth genres = validated_data.get('genre') for gnr in genres: name = gnr.get('name') try: genre = Genre.objects.get(name=name) except Genre.DoesNotExist: genre = Genre.objects.create(name=name) book.genre.add(genre) book.save() mail_to_admin.delay() return book def update(self, instance, validated_data): instance.title = validated_data.get('title', instance.title) b_auth = validated_data.get('author', instance.author).get('name') try: auth = Author.objects.get(name=b_auth) except Author.DoesNotExist: auth = Author.objects.create(name=b_auth) instance.author = auth genres = validated_data.get('genre', instance.genre) for gnr in instance.genre.all(): instance.genre.remove(gnr) for gnr in genres: name = gnr.get('name') try: genre = Genre.objects.get(name=name) except Genre.DoesNotExist: genre = Genre.objects.create(name=name) instance.genre.add(genre) instance.year = validated_data.get('year', instance.year) instance.price = validated_data.get('price', instance.price) instance.save() return instance class Meta: model = Book fields = ['id', 'title', 'author', 'genre', 'year', 'price',]
class PublicationSerializer(serializers.ModelSerializer): authors = AuthorSerializer(many=True, read_only=True) class Meta: model = Publications fields = [ 'id', 'venue', 'year', 'volume', 'doi', 'title', 'abstract', 'n_citations', 'semantic_scholar_id', 'authors' ] read_only = [ 'id', 'venue', 'year', 'volume', 'doi', 'title', 'abstract', 'n_citations', 'semantic_scholar_id', 'authors' ]
class ArticleDetailSerializer(serializers.ModelSerializer): author = AuthorSerializer() subject = SubjectSerializer() class Meta: fields = ( "id", "slug", "title", "hero_image", "author", "subject", "publish_date", "text", ) model = Article
class ArticleListSerializer(serializers.ModelSerializer): text = serializers.ReadOnlyField(source="truncate_text") author = AuthorSerializer() subject = SubjectSerializer() class Meta: fields = ( "id", "slug", "title", "hero_image", "author", "subject", "publish_date", "text", ) model = Article
class ProjectSerializer(ModelSerializer): members = AuthorSerializer(many=True, read_only=True) class Meta: model = Project fields = '__all__'