Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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')
        ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading