Changed work of shelf books
This commit is contained in:
@@ -1,26 +1,17 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from apps.shelves.serializers import ShelfSerializer
|
||||
from apps.authors.serializers import AuthorSerializer
|
||||
from apps.books.models import Book
|
||||
from apps.shelves.models import Shelf
|
||||
from apps.authors.models import Author
|
||||
|
||||
class BookSerializer(serializers.ModelSerializer):
|
||||
shelf = ShelfSerializer(read_only=True)
|
||||
shelf_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Shelf.objects.all(),
|
||||
source="shelf",
|
||||
write_only=True
|
||||
)
|
||||
|
||||
authors = AuthorSerializer(many=True, read_only=True)
|
||||
authors_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Author.objects.all(),
|
||||
source="authors",
|
||||
many=True,
|
||||
allow_null=True,
|
||||
write_only=True
|
||||
write_only=True,
|
||||
required=False
|
||||
)
|
||||
|
||||
class Meta:
|
||||
@@ -28,27 +19,23 @@ class BookSerializer(serializers.ModelSerializer):
|
||||
fields = [
|
||||
"id",
|
||||
"name",
|
||||
"isbn",
|
||||
"shelf",
|
||||
"shelf_id",
|
||||
"isbn",
|
||||
"authors",
|
||||
"authors_id"
|
||||
]
|
||||
read_only_fields = ["id"]
|
||||
|
||||
|
||||
def __init__(self, instance=None, data=..., **kwargs):
|
||||
super().__init__(instance, data, **kwargs)
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
request = self.context.get("request")
|
||||
if request and request.user:
|
||||
self.fields["shelf_id"].queryset = Shelf.objects.filter(
|
||||
user=request.user
|
||||
)
|
||||
|
||||
if request and request.user and request.user.is_authenticated:
|
||||
self.fields["authors_id"].queryset = Author.objects.filter(
|
||||
user=request.user
|
||||
)
|
||||
else:
|
||||
self.fields["authors_id"].queryset = Author.objects.none()
|
||||
|
||||
|
||||
def create(self, validated_data):
|
||||
|
||||
Reference in New Issue
Block a user