Home > database >  Drf: how to throttle a create request based on the amount of request's made in general and not
Drf: how to throttle a create request based on the amount of request's made in general and not

Time:03-13

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

CodePudding user response:

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')
        ]
  • Related