This is a digital library system. When borrow end_date has expired, borrow status will be updated automatically to 1 (End), table book column stock will be updated adding one stock, and automatically inserting new content value in table notification.
class Borrow(models.Model):
status = models.CharField(max_length=1, choices=[('0', 'Process'),('1', 'End')], default=0)
book = models.ForeignKey('models.Book', on_delete=models.CASCADE, related_name='borrow_book')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
end_date = models.DateTimeField()
class Book(models.Model):
stock = models.IntegerField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Notification(models.Model):
content = models.CharField(max_length=255, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
How to make an automatic update query in table borrow column status, table book column stock, and insert in table notification when end_date > DateTime.now() ?
I would make a management command and then create a Cron / Scheduled Task to run it Hourly.
There are packages like
django-crontabanddjango-cronthat could also do this (I assume?), but I've never used either and have stuck with the ol' Default Linux CrontabNote: I left out creating Notification
Management Command
This would probably be a better way to update the count, especially if there's multiple barrows for a single book.
But this might not be the best idea because a book could be late to be returned and then your count is off.