简体   繁体   English

如何在Django中设置模型限制

[英]How to set limit on models in Django

I have a little problem. 我有一点问题。 I want to set the limit for users in my app. 我想为我的应用程序中的用户设置限制。 For example: The new user after register and login to his account can add only one record. 例如:新用户注册并登录帐户后,只能添加一条记录。 After added if this same user want to add next record, the app will return the alert "You are permited to add only one record". 添加后,如果同一用户要添加下一条记录,则该应用将返回警报“您仅允许添加一条记录”。 How to solve it? 怎么解决呢?

You need to have a way to remember if the user used their limit or not. 您需要一种方法来记住用户是否使用了他们的限制。 I know 2 options here: 我在这里知道2种选择:

  1. Add a bool/int field to the user that remembers if/how many records that user created. 向用户添加一个bool / int字段,以记住该用户是否创建了多少条记录。

This is the best tutorial I have found so far regarding extending user model with some additional data: https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html 这是迄今为止我找到的关于使用一些附加数据扩展用户模型的最佳教程: https : //simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html

  1. Alternatively, you could create a foreign key in a forementioned "record" to the user (eg called "creator"). 或者,您可以在给用户的上述“记录”中创建外键(例如,称为“创建者”)。 The reverse link from user to the record would give you the records created by this user. 从用户到记录的反向链接将为您提供该用户创建的记录。

No matter which solution works better for you, you'll need to check the conditions in the creation view and throw an error if the limit would be exceeded (eg PermissionDenied ) 无论哪种解决方案对您更有效,您都需要在创建视图中检查条件,如果超出限制(例如PermissionDenied ),则会抛出错误。

You can build a model similar to this that defines the limit per user and validating the inserted data using a classmethod : 您可以构建类似于此模型的模型,该模型定义每个用户的限制并使用classmethod验证插入的数据:

class LimitedRecord(models.Model):
    limits = 5
    user = models.ForeignKey(User)
    data = models.CharField(max_length=128)

    @classmethod
    def add_record(cls, user, data):
        if not cls.can_add(user):
            raise Exception('Not Allowed')
        instance = cls.objects.create(user=user, data=data)
        return instance

    @classmethod
    def can_add(cls, user):
        return cls.objects.filter(user=user).count() <= cls.limits:

I separated the login here into two methods, but sure you can combine them in the same unit that adds the record. 我在这里将登录分为两种方法,但是请确保您可以将它们合并到添加记录的同一单元中。

Add two fields to your User model: User模型中添加两​​个字段:

class User(models.Model):
    n_records_permitted = models.PositiveIntegerField(default=1)
    n_records_added = models.PositiveIntegerField(default=0)

Now either don't display the relevant form(s) for Users that have user.n_records_added >= user.n_records_permitted or include this check in the form's validation. 现在,要么不显示具有user.n_records_added >= user.n_records_permitted用户的相关表单,要么不将其包括在表单的验证中。

And of increase both counters as appropriate. 并且适当增加两个计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM