from django.db.models import Q, F from django.db import models from apps.books.models import Book from apps.shelves.models import Shelf class ShelfBook(models.Model): book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name="shelf_items") shelf = models.ForeignKey(Shelf, on_delete=models.CASCADE, related_name="book_items") quantity = models.PositiveIntegerField(default=0) class Meta: constraints = [ models.UniqueConstraint( fields=["book", "shelf"], name="unique_book_per_shelf" ), ]