Django:无法使用 m2m_changed 信号检测多对多字段的更改 - 在模型级别进行审核

发布于 2024-10-12 15:16:54 字数 961 浏览 2 评论 0原文

我想跟踪任何模型上哪些字段发生了变化(即在模型级别进行审计,因为它更原子,而不是像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人如初 2024-10-19 15:16:54

我有类似的问题,我想我可以解决它。我不知道你是如何使用 m2m_changed 但它应该在 models.py 上并且应该类似于这样:

signals.m2m_changed.connect(your_function, sender=CustomGroup.users.through)

现在,我将创建一个包含该函数的 signal.py 文件,下面的代码应该打印你您选择的选项:

def your_function(sender, instance, action, reverse, model, pk_set, **kwargs):
    if action == 'post_add':
        for val in pk_set:
            print val

现在,您知道更新的值。我希望这可以解决您的问题。

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:

signals.m2m_changed.connect(your_function, sender=CustomGroup.users.through)

Now, I would create a signals.py file containing that function, and the following code should print you the options that you have selected:

def your_function(sender, instance, action, reverse, model, pk_set, **kwargs):
    if action == 'post_add':
        for val in pk_set:
            print val

Now, you know the updated values. I hope this could solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文