django m2m_changed通过模型自定义 [英] django m2m_changed with custom through model

查看:87
本文介绍了django m2m_changed通过模型自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,我确实有两个模型 Author和 Publication与多对多字段相关联,因此我可以为出版物分配不同的作者。另外,我必须使用自定义的贯穿模型作者来定义正确的顺序。

In Django I do have two models "Author" and "Publication" that are connected with a Many-to-Many-Field, so that I can assign different authors to a publication. Additionally, I have to use a custom through-model "Authorship" to define the correct order.

class Author(models.Model):
    first_name = models.CharField(max_length=48)
    .....


class Authorship(models.Model):
    author = models.ForeignKey(Author)
    publication = models.ForeignKey('Publication')
    order_of_authorship = models.IntegerField(default=1)


class Publication(models.Model):
    title = models.CharField(max_length=128)
    authors = models.ManyToManyField(Author, through=Authorship)
    year = models.IntegerField(max_length=4)
    ...

    citation_key = models.CharField(max_length=9, blank=True, default="")

此刻,我使用管理界面使用发布表单和内联表单 Authorship填充数据。

At the moment I use the Admin Interface to populate my data with a form for the "Publication" and an inline form "Authorship".

我现在想要实现的目标:
另一个citation_key-field(例如 Ei

What I want to achieve now: An additional citation_key-field (e.g. "Einstein1950") should be auto-populated after data has changed.

我试图做的事情:
我发现使用信号必须是最佳做法。

What I tried to do: I found out that using signals must be the best practice.

但是,当我更改作者身份时, Publication.authors.through上的 m2m_changed信号不会被触发。

However the "m2m_changed"-Signal on "Publication.authors.through" is not fired, when I change the Authorships.

@receiver(m2m_changed, sender=Publication.authors.through)
def authors_changed(sender, **kwargs):
    print("authors changed")

此问题也在相关主题,在其中作者似乎在贯穿模型中使用了 post_save。

This problem is also discussed in a related topic, where the author seems to use "post_save" on the through-model.

@receiver(post_save, sender=Authorship)
def authorship_changed(sender, instance, **kwargs):
    print("authors changed")

这似乎可以解决,但我要记住,删除尚未涵盖,所以我添加了一个post_delete信号:

This seems to work out, but I have to keep in mind, that a deletion is not covered yet, so I added a post_delete-signal:

@receiver(post_delete, sender=Authorship)
def authorship_deleted(sender, instance, **kwargs):
    print("authors deleted")



<现在的问题是:如果我添加4位作者,则该事件被触发4次。如果我想按上述方式更新我的citation_key,也会发生4次。

The problem now is: If I add 4 authors, I get that event fired 4 times. If I want to update my citation_key as described before, this happens also 4 times.

这是正确的解决方案吗?还是有更好的最佳实践?我认为它必须以某种方式与m2m_changed信号一起工作,但我不知道如何。
因为我是Django的新手,所以我不知道这对您来说是否是显而易见的解决方案。此外,在这种情况下,不必要的计算应该不会产生很大的影响,但这一点也不好。

Can this be the correct solution? Or is there a better best practice? I assume it must work somehow with the m2m_changed signal, but I don't know how. Since I am new to Django, I don't know if this is the obvious solution for you. Furthermore, in this scenario the unnecessary calculation should not have a huge impact, but it is not nice at all.

我只发现了一个非常古老的 bug报告似乎也解决了这个问题。

I only found a really old bug-report in the Django-Trac that seems to address this problem as well. But there is not solution yet.

推荐答案

这是一个已知的错误,报告为 ticket 17688 在Django上。

This is a known bug, reported as ticket 17688 on Django.

这篇关于django m2m_changed通过模型自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆