In the Book model, I made 2 methods to call them in the Borrowing field, but I haven't figured out how exactly to do it. And it is especially not clear how to connect the logic of returning the book. In the Borrowing model, there is only the actual_return_date field, when it is filled in, 1 should be added to the inventory, I think so. I tried to change the create method but it didn't work.
model Book:
from django.db import models
class Book(models.Model):
COVER_CHOICES = [("HARD", "Hard cover"), ("SOFT", "Soft cover")]
title = models.CharField(max_length=255)
authors = models.CharField(max_length=256)
cover = models.CharField(max_length=15, choices=COVER_CHOICES)
inventory = models.PositiveIntegerField()
daily_fee = models.DecimalField(max_digits=7, decimal_places=2)
class Meta:
ordering = ["title"]
def __str__(self):
return (
f"'{self.title}' by {self.authors}, "
f"cover: {self.cover}, "
f"daily fee: {self.daily_fee}, "
f"inventory: {self.inventory}"
)
def reduce_inventory_book(self):
self.inventory -= 1
self.save()
def increase_inventory_book(self):
self.inventory = 1
self.save()
book/view.py
from rest_framework import viewsets
from book.models import Book
from book.serializers import BookSerializer
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
book/serializers.py
from rest_framework import serializers
from book.models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ("id", "title", "authors", "cover", "inventory", "daily_fee")
model Borrowing:
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
from book.models import Book
class Borrowing(models.Model):
borrow_date = models.DateField(auto_now_add=True)
expected_return_date = models.DateField()
actual_return_date = models.DateField(null=True)
book = models.ForeignKey(Book, on_delete=models.PROTECT, related_name="borrowings")
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name="borrowings"
)
class Meta:
ordering = ["borrow_date"]
def __str__(self):
return f"{self.borrow_date}"
borrowing/views.py
from rest_framework import viewsets
from borrowing.models import Borrowing
from borrowing.serializers import BorrowingSerializer
class BorrowingViewSet(viewsets.ModelViewSet):
queryset = Borrowing.objects.all()
serializer_class = BorrowingSerializer
def get_queryset(self):
queryset = self.queryset
if not self.request.user.is_staff:
queryset = queryset.filter(user=self.request.user)
user_id = self.request.query_params.get("user_id")
is_active = self.request.query_params.get("is_active")
if str(self.request.user.id) == user_id and is_active:
queryset = queryset.filter(actual_return_date=None)
return queryset
def perform_create(self, serializer):
serializer.save(user=self.request.user)
serializer.save(book=self.request.book)
borrowing/serializers.py
from rest_framework import serializers
from book.models import Book
from book.serializers import BookSerializer
from borrowing.models import Borrowing
class BorrowingSerializer(serializers.ModelSerializer):
class Meta:
model = Borrowing
fields = (
"id",
"borrow_date",
"expected_return_date",
"actual_return_date",
"book",
)
def create(self, validated_data):
borrowing = Borrowing.objects.create(**validated_data)
borrowing.book.reduce_inventory_book()
return borrowing
I think that I need to change the credit method for this, but I don’t know how.
CodePudding user response:
You can alter the save method of Borrowing so that when actual_return_date is filled to call the increase_inventory_book method