Django:无法使用 m2m_changed 信号检测多对多字段的更改 - 在模型级别进行审核
我想跟踪任何模型上哪些字段发生了变化(即在模型级别进行审计,因为它更原子,而不是像 django 和 django-reversion 已经可以做的那样在管理/表单级别进行审计)。我可以使用前/后保存/删除信号对任何字段执行此操作。但是,我在 m2m 领域执行此操作时遇到问题。
对于下面的代码示例,我在用户更改表单中定义了“custom_groups”m2m 字段,因为它是反向关系。例如,当用户在管理界面上保存表单时,我想记录“custom_groups”字段中是否有更改。
模型:
from django.contrib.auth.models import User
class CustomGroup(models.Model):
users = models.ManyToManyField(User, related_name='custom_groups')
ModelForm:
class CustomUserChangeForm(UserChangeForm):
custom_groups = forms.ModelMultipleChoiceField(required=False, queryset=CustomGroup.objects.all())
使用 m2m_changed 信号的问题是,我无法检查使用赋值运算符更新 m2m 字段的情况下实际发生了什么变化:
user.custom_groups = self.cleaned_data['custom_groups']
这是因为在手动添加所有对象之前,django 内部会在 *custom_groups* 上执行clear()。这将在 m2m 字段上执行预/后清除,然后执行预/后保存。
我做这一切的方式都是错误的吗?有没有更简单、实际可行的方法?
谢谢!
I'd like to keep track on what field has changed on any model (i.e. audit at model level since it's more atomic, not at admin/form-level like what django and django-reversion can already do). I'm able to do that for any field using pre/post save/delete signals. However, I have a problem of doing that on an m2m field.
For the code sample below, i define 'custom_groups' m2m field in user change form since it's a reverse relation. When user saves the form on admin interface for example, I'd like to log if there's a change in 'custom_groups' field.
Model:
from django.contrib.auth.models import User
class CustomGroup(models.Model):
users = models.ManyToManyField(User, related_name='custom_groups')
ModelForm:
class CustomUserChangeForm(UserChangeForm):
custom_groups = forms.ModelMultipleChoiceField(required=False, queryset=CustomGroup.objects.all())
The problem with using m2m_changed signal is that i can't check what has actually changed for the case where the m2m field is updated using assignment operator:
user.custom_groups = self.cleaned_data['custom_groups']
This is because internally django will perform a clear() on *custom_groups*, before manually adding all objects. This will execute pre/post-clear and then pre/post save on the m2m field.
Am I doing all this the wrong way? Is there a simpler method that can actually work?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有类似的问题,我想我可以解决它。我不知道你是如何使用 m2m_changed 但它应该在 models.py 上并且应该类似于这样:
现在,我将创建一个包含该函数的 signal.py 文件,下面的代码应该打印你您选择的选项:
现在,您知道更新的值。我希望这可以解决您的问题。
I had a similar problem and I think I could solve it. I don't know how you are using the m2m_changed but it should be on models.py and should be similar to something like this:
Now, I would create a signals.py file containing that function, and the following code should print you the options that you have selected:
Now, you know the updated values. I hope this could solve your problem.