How to make the inventory decrease by 1 book when I borrow from the library, and increase by 1 when I return the book?

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

    def increase_inventory_book(self):
        self.inventory += 1


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


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}"


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( == user_id and is_active:
            queryset = queryset.filter(actual_return_date=None)

        return queryset

    def perform_create(self, serializer):


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 = (

    def create(self, validated_data):
        borrowing = Borrowing.objects.create(**validated_data)
        return borrowing

I think that I need to change the credit method for this, but I don’t know how.

You can alter the save method of Borrowing so that when actual_return_date is filled to call the increase_inventory_book method

Back to Top