跟踪django模型实例上的脏字段
caldwell-dirtyfields的Python项目详细描述
跟踪django模型实例上更改的字段。
使一个MIXIN可用,它将给你属性:
- ^{tt1}$
- ^{tt2}$
安装
使用pip安装包。然后使用“在模型中使用MIXIN”的说明。
$ pip install django-dirtyfields
或者如果您对开发它感兴趣,请使用virtualenv。默认的settings.py将在其当前位置查找dirtyfields包。
$ virtualenv --no-site-packages ve/ $ source ve/bin/activate (ve)$ cd example_app (ve)$ pip install -r requirements.pip (ve)$ ./manage.py test testing_app
<H2>在模型中使用MIXIN
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)
在外壳中使用它
(ve)$ ./manage.py shell >>> from testing_app.models import TestModel >>> tm = TestModel(boolean=True, characters="testing") >>> tm.save() >>> tm.is_dirty False >>> tm.dirty_fields () >>> tm.boolean = False >>> tm.is_dirty True >>> tm.dirty_fields ('boolean', ) >>> tm.characters = "have changed" >>> tm.is_dirty True >>> tm.dirty_fields ('boolean', 'characters', ) >>> tm.save() >>> tm.is_dirty False >>> tm.get_dirty_fields ()
学分
这段代码很大程度上是根据Stack Overflow提供的内容改编的。