Changed work of shelf books
This commit is contained in:
28
backend/apps/books/migrations/0001_initial.py
Normal file
28
backend/apps/books/migrations/0001_initial.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# Generated by Django 6.0.3 on 2026-03-22 12:16
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('authors', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Book',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=255)),
|
||||
('isbn', models.CharField(max_length=13)),
|
||||
('authors', models.ManyToManyField(related_name='books', to='authors.author')),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='books', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -1,6 +1,5 @@
|
||||
from django.db import models
|
||||
|
||||
from apps.shelves.models import Shelf
|
||||
from apps.authors.models import Author
|
||||
from apps.user.models import User
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -14,6 +14,5 @@ class BooksViewSet(viewsets.ModelViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return self.queryset.filter(
|
||||
shelf__user_id=self.request.user.pk
|
||||
).prefetch_related("authors")
|
||||
return self.queryset.filter(user=self.request.user)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user