python - DRF : how to throttle a create request based on the amount of request's made in general and not per user

标签 python django django-rest-framework

我正在制作一个出勤系统,在这个系统中,教师和被允许的学生可以参加他们的类(class),我希望每天一次,如果学生已经参加了,那么老师就不能参加了。

出勤模型

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

最佳答案

您可以使 StudentDate 的组合与 UniqueConstraint [Django-doc] 一起唯一:

class Attendance(models.Model):
    # …

    class Meta:
        constraints = [
            models.UniqueConstraint(<strong>fields=('Student', 'Date')</strong>, name='student_once_per_date')
        ]

关于python - DRF : how to throttle a create request based on the amount of request's made in general and not per user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71454967/

相关文章:

python - 我在 PySide 1.1.0 中找不到 QString

python - Linux 输出重定向守护进程不工作

django - 我应该对我的所有应用程序运行 Django "makemigrations"吗?

django - 需要帮助理解序列化程序中的许多和源字段

python - 如何使用 Python 解压缩 gz 文件

python - Django编程报错1146表不存在

Django:如何存储基于子域的身份验证用户名?

Django:安全中间件使站点崩溃

Django rest swagger post 参数未显示在 Swagger 文档上

python - 如何使用 django-rest-framework 从端点检索 Json 元数据