rediet yosef
rediet yosef

Reputation: 222

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

Upvotes: 3

Views: 74

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

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

Upvotes: 1

Related Questions