V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
gavin3318
V2EX  ›  Python

django 有没有什么监控 model 字段修改的插件。

  •  
  •   gavin3318 · 2019-07-29 15:02:31 +08:00 · 1612 次点击
    这是一个创建于 1704 天前的主题,其中的信息可能已经有所发展或是发生改变。

    某个字段修改,记录下修改人 修改前后的值, 现在我在 ModelForm clean 函数里处理了下,感觉 。。。。挺蛋疼的。

    class SynthesisStepForm(BootstrapModelForm):
        class Meta:
            model = SynthesisStep
            fields = ('title', 'version', 'condition', 'yield_rate', 'testing', 'stype')
            widgets = {
                'testing': forms.Textarea(attrs={'rows': '4', 'cols': '100', 'style': 'width:auto;'}),
                'condition': forms.Textarea(attrs={'rows': '4', 'cols': '100', 'style': 'width:auto;'})
            }
    
        def clean(self):
            """
            用于监控字段被修改
            :return:
            """
            update_fields = ('title', 'version', 'condition', 'yield_rate', 'testing', 'stype')
            cleaned_data = super(BootstrapModelForm, self).clean()
            update_note = supervisory_update_fields(self, cleaned_data, update_fields)
            self._update_note = update_note
    
            return cleaned_data
    
    def supervisory_update_fields(form_obj, cleaned_data, update_fields):
        """
        用于监控 form 提交字段的变化
        :param form_obj:
        :param cleaned_data:
        :param update_fields:
        :return:
        """
        update_note = ''
        if form_obj.instance.pk:  # new instance only
            changed_data = form_obj.changed_data
            for data in changed_data:
                if data in update_fields:
                    try:
                        old_field = eval('form_obj.instance.{0}'.format(data))
                        new_field = cleaned_data[data]
                        if old_field != new_field:
                            update_note = update_note + '{2} is modified , from "{0}" to "{1}"\n'.format(old_field,new_field, data)
                    except Exception as e:
                        pass
    
        return update_note
    
    
    3 条回复    2019-07-29 18:49:27 +08:00
    izoabr
        1
    izoabr  
       2019-07-29 15:49:26 +08:00
    内容版本管理,我记得有好几个现成的,刚搜了一下:
    AuditTrail
    django-reversion
    noobsheldon
        2
    noobsheldon  
       2019-07-29 16:20:22 +08:00 via Android
    django signal
    37Y37
        3
    37Y37  
       2019-07-29 18:49:26 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4537 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:07 · PVG 18:07 · LAX 03:07 · JFK 06:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.