'Drf: how to throttle a create request based on the amount of request's made in general and not per user
I was making an attendance system in which teachers and permitted students can take attendance of their class, I want it to be once per day and if the student has already taken the attendance the teacher should not be able to.
attendance model
class Attendance(models.Model):
Choices = (
("P", "Present"),
("A", "Absent"),
("L", "On leave"),
)
Student = models.ForeignKey(
User, on_delete=models.CASCADE, blank=False, null=True)
leave_reason = models.CharField(max_length=355, blank=True, null=True)
Date = models.DateField(blank=False, null=True,
auto_now=False, auto_now_add=True)
Presence = models.CharField(
choices=Choices, max_length=255, blank=False, null=True)
def __str__(self):
return f'{self.Student}'
Solution 1:[1]
You can make the combination of Student and Date unique together with a UniqueConstraint [Django-doc]:
class Attendance(models.Model):
# …
class Meta:
constraints = [
models.UniqueConstraint(fields=('Student', 'Date'), name='student_once_per_date')
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
