5

我正在应用 post_save 信号来应用每个对象的用户权限,然后相应地过滤查询集。

我的模型是这样的:

class Project(models.Model):
    # Relations with other entities.
    employees = models.ManyToManyField('staff.Person', through='project.PersonProjectMembership',
                                       related_name='projects')
    research_groups = models.ManyToManyField('group.Group', related_name='projects',
                                             through='project.ProjectGroupMembership')
    departments = models.ManyToManyField('department.Department', related_name='projects',
                                         through='project.ProjectDepartmentMembership')

问题是:当我收到保存后信号时,虽然我输入了部门、研究组和员工的值,但它们似乎总是空的。有什么我错过的吗?

更新:在当前代码下方,尚未按预期工作。我已将 post_save 更改为 m2m_changed。

from django.db.models.signals import m2m_changed
from django.db import models
from django.dispatch.dispatcher import receiver

class Project(models.Model):
    employees = models.ManyToManyField('staff.Person', through='project.PersonProjectMembership',
    related_name='projects')

class PersonProjectMembership(models.Model):
    project = models.ForeignKey('project.Project', related_name="person_memberships")
    person = models.ForeignKey('staff.Person', related_name="project_memberships")
    lead = models.BooleanField(default=False)
    position = models.CharField(max_length=50)
    project_manager = models.BooleanField(
        default=False
    )

    class Meta:
        permissions = (
            ('view_personprojectmembership', _('View person project membership')),
        )

@receiver(m2m_changed, sender=Project.employees.through)
def _on_save_project_assign_privileges(sender, instance, action, reverse, model, pk_set, using, **kwargs):
    # [...]

解决方案

在我的项目模型中,我明确将 PersonProjectMembership 定义为 m2m 关系员工中的中间模型:

class Project(models.Model):
    # Relations with other entities.
    employees = models.ManyToManyField('staff.Person', through='project.PersonProjectMembership', related_name='projects')

我保存项目时的时间线如下:

  1. 项目.save()
  2. PersonProjectMembership.save()

因此,在 Project.post_save 上,员工仍然为空是正常的。我必须做的是听 PersonProjectMembership post_save 信号:

@receiver(post_save, sender=PersonProjectMembership)
def my_listener(**kwargs):
    # do stuff [...]

查看https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.ManyToManyField.throughhttps://docs.djangoproject.com/en/1.9/topics/signals /

4

2 回答 2

2

在对我的代码进行了大量挖掘并进行了简单的测试之后,我按照 krasnoperov 的建议尝试了 m2m_changed 信号。我意识到,如果您明确声明“通过”模型(在我的例子中为“PersonProjectMembership”),此信号将无法正常工作。

然后我又想了想,将我的方法与“PersonProjectMembership”的 post_save 信号联系起来。这完美无缺。

于 2013-06-28T13:05:10.023 回答
1

您可能知道,Many2Many 关系是通过附加表存储的,其中包含关系两端的主键。因此,使用 Many2Many 关系保存模型实例是两个步骤:

  1. 首先,实例被保存:数据库中的新记录被创建并且实例接收它的主键。post_save此刻被解雇。

  2. 之后,关系被保存:关系表中的记录被创建。m2m-changed此时发出信号。

换句话说,当post_save被解雇时,m2m 关系尚未处理。

您可以查看文档:m2m-changed

于 2013-06-17T21:12:33.340 回答