跟踪django模型实例上的脏字段(主动维护)
django-dirtyfields的Python项目详细描述
跟踪django模型实例上的脏字段。 脏表示内存中的字段和数据库值不同。
此软件包与Django的最新版本(1.8、1.9、1.10、1.11系列)兼容并经过测试。
安装
$ pip install django-dirtyfields
用法
要使用django-dirtyfields,您需要:
- 从要跟踪的django模型中的DirtyFieldsMixin继承。
from django.db import models from dirtyfields import DirtyFieldsMixin class TestModel(DirtyFieldsMixin, models.Model): """A simple test model to test dirty fields mixin with""" boolean = models.BooleanField(default=True) characters = models.CharField(blank=True, max_length=80)
在模型实例上使用这两个函数之一,以了解该实例是否脏,并获取脏字段:
- is_dirty()
- get_dirty_fields()
示例
>>> from tests.models import TestModel >>> tm = TestModel.objects.create(boolean=True,characters="testing") >>> tm.is_dirty() False >>> tm.get_dirty_fields() {} >>> tm.boolean = False >>> tm.is_dirty() True >>> tm.get_dirty_fields() {'boolean': True}
有关更多信息,请参阅full documentation。