推荐学习书目
› 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 · Jul 29, 2019 · 2584 views
    This topic created in 2623 days ago, the information mentioned may be changed or developed.

    某个字段修改,记录下修改人 修改前后的值, 现在我在 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 replies  •  2019-07-29 18:49:27 +08:00
    izoabr
        1
    izoabr  
       Jul 29, 2019
    内容版本管理,我记得有好几个现成的,刚搜了一下:
    AuditTrail
    django-reversion
    noobsheldon
        2
    noobsheldon  
       Jul 29, 2019 via Android
    django signal
    37Y37
        3
    37Y37  
       Jul 29, 2019
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2256 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 06:23 · PVG 14:23 · LAX 23:23 · JFK 02:23
    ♥ Do have faith in what you're doing.