Home > Back-end >  getting the count of a query set in Django
getting the count of a query set in Django

Time:09-16

i am making a doctor appointment system, and i want to get the count of how many appointment i have in a specific day, so instead of doing it this way:

appointments_count = Appointment.objects.filter(user = user, date = specific_date).count()

will it be more efficient if i make another table for keeping track of the count of the appointments for each date, to avoid more i/o disk operations

class Date_Counter(models.Model):
    doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
    date = models.DateField()
    count = models.PositiveIntegerField(default=1)

and thanks everyone

CodePudding user response:

Never hard code fields that can be obtained with a query (this is a fundamental rule of relational databases). If you use the second approach, you need to always update the count, so the performance is also worst.

CodePudding user response:

I would add a count filed to the Doctor Model and use Django signals to update the count each time a new appointment is created

  • Related