简体   繁体   English

django inlineformset_factory,保存时查看更改的字段

[英]django inlineformset_factory, view changed fields on save

I am creating some custom validation, that on save checks if certain fields values have changed. 我正在创建一些自定义验证,该验证会在保存时检查某些字段值是否已更改。

On POST , is there a way in the view that I can check what fields have changed for that formset? POSTview中是否有办法可以检查该表单集的哪些字段已更改?

I am currently looping through the formset and am able to view individual field values, so I could compare them to a query. 我目前正在遍历表单集,并且能够查看各个字段值,因此可以将它们与查询进行比较。 It just seems to be a bit more than necessary considering the formset doesn't go through the save process if nothing has changed. 考虑到如果没有任何更改,表单集不会通过保存过程,这似乎超出了必要。

Any help is appreciated. 任何帮助表示赞赏。

Add a method to your formset: 将方法添加到您的表单集中:

def has_changed()
    return all([form.has_changed() for form in self.forms])

Similarly, the form also documents the changed_fields, which you can concatenate in your formset. 同样,该表单还记录了changed_fields,您可以在表单集中将其串联。

I don't think formset.save() blindly overwrites all entries into the db. 我认为formset.save()不会盲目地将所有条目覆盖到db中。 It checks the changed forms, which I think is available in formset.changed_forms and it updates only those rows in the db. 它检查更改后的表单,我认为可以在formset.changed_forms它,并且仅更新数据库中的那些行。

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

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