简体   繁体   English

如何在 Django 中实现经过验证的字段?

[英]How can I implement a verified field in django?

In Django I'd like to add a field "verified" of type BooleanField to my models which shall indicate if the current model instance has been reviewed by a staff user member or not.在 Django 中,我想在我的模型中添加一个BooleanField类型的“已验证”字段,该字段应指示当前模型实例是否已由员工用户成员审查。 Whenever a model instance field other than the "verified" field changed the verified field value shall be reset to False .每当“已验证”字段以外的模型实例字段更改时,已验证字段值应重置为False Whenever only the "verified" field has been changed it's value shall be taken as is (most of the time True but potentially False as well).每当只有“已验证”字段发生更改时,它的值应按原样采用(大多数情况下为True但也可能为False )。

One possibility would be to reset the "verified" field in post-save signals handlers considering update_fields passed to save() .考虑update_fields传递给save() update_fields一种可能性是重置post-save信号处理程序中的“已验证”字段。 However using signals seems to be considered an anti-pattern in almost all use cases.然而,在几乎所有用例中,使用信号似乎都被认为是一种反模式。 Instead one should override the save() method.相反,应该覆盖save()方法。 But still when overriding save I'd have to determine update_fields manually somehow.但是在覆盖save我仍然必须以某种方式手动确定update_fields Otherwise I've no information about which fields changed.否则我没有关于哪些字段发生变化的信息。

How can I implement something like this most easily.我怎样才能最容易地实现这样的事情。 I'd prefer a solution using a third-party package wo custom hacks or a solution without any dependencies to other packages.我更喜欢使用第三方包的解决方案 wo 自定义 hacks 或与其他包没有任何依赖关系的解决方案。 However using django-model-utils monitorfield , django-dirtyfields for a custom implementation or something equivalent would be ok as well.但是,使用django-model-utils monitorfielddjango-dirtyfields用于自定义实现或等效的东西也可以。

Using dirty-fields seems to be easiest to implement a verified field.使用dirty-fields似乎最容易实现经过验证的字段。 So far I came up with something like follows:到目前为止,我想出了如下内容:

DJANGO-APP/models.py : DJANGO-APP/models.py :

from django.db import models
from dirtyfields import DirtyFieldsMixin


class VerifiedModel(DirtyFieldsMixin, models.Model):
    """
    Abstract class which allows to extend models with user verification model field.
    """
    ENABLE_M2M_CHECK = True
    verified = models.BooleanField(
        default=False,
        help_text="The verification status. True means meta-data is verified. False means meta-data is not verified. The verification status is reset to False whenever a field is set.",
    )

    def _update_verified_field(self):
        """
        To be called in inheriting model's save() method.
        """
        if self.is_dirty():
            if not 'verified' in self.get_dirty_fields():
                self.verified = False

    class Meta:
        abstract = True


class ModelToBeVerified(VerifiedModel):

    ...

    def save(self, *args, **kwargs):
        ...
        self._update_verified_field()
        return super(ModelToBeVerified, self).save(*args, **kwargs)

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

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