Django M2M通过int“自我”。表,如何获取相关项目

时间:2022-10-04 15:23:22

I am trying to establish some M2M relation in one of my models. Django asked me to put related_name argument to some of the fields, and I put them. However, I am now confused with how to get related items. Here is my models simplified.

我试图在我的一个模型中建立一些M2M关系。 Django让我把related_name参数放到一些字段中,然后我就把它们放了。但是,我现在对如何获取相关项目感到困惑。这是我简化的模型。

class Post(models.Model):
    # other fields
    relevancy = models.ManyToManyField("self",through="Traffic",symmetrical=False,related_name="relevant",blank=True)

    #some method definitions

class Traffic(models.Model):
    tfrom = models.ForeignKey(Post,related_name="tfrom")
    tto = models.ForeignKey(Post,related_name="tto")
    count = models.PositiveIntegerField(default=0)

This is to be a user tendency analyzing, and giving suggestions to other users tool. The part I am confused is the part where I get related post to the post in question. For example, when I choose Post, how do I get a set of post that made traffic from that post, or set of posts that trafficked to that post etc. Here is my related question that I posted before.

这是一种用户倾向分析,并向其他用户工具提供建议。我感到困惑的部分是我得到相关帖子的部分。例如,当我选择发布时,如何获得一组帖子,该帖子来自该帖子,或者一组帖子被贩运到该帖子等。这是我之前发布的相关问题。

PS: English is not my main language, sorry if my question is hard to understand.

PS:英语不是我的主要语言,对不起,如果我的问题很难理解。

1 个解决方案

#1


1  

This depends on the intermediate model definition. The firs FK of the intermediate model is what defines the 'principal' access, so

这取决于中间模型定义。中间模型的第一个FK定义了“主要”访问,因此

a_post.relevancy.all()

is the set of all the Post instances with a traffic from a_post to them .

是从a_post到它们的流量的所有Post实例的集合。

a_post.relevant.all()

is the set of all the Post instances with a traffic from them to a_post.

是所有Post实例的集合,具有从它们到a_post的流量。

You should really think about your naming conventions, it's pretty confusing. Something in the line of

你应该考虑一下你的命名约定,这很令人困惑。有点像

links = models.ManyToManyField("self",
             through="Traffic",
             symmetrical=False,
             related_name="referrers",
             blank=True)

The key is in the django sorce code:

关键在于django sorce代码:

https://code.djangoproject.com/browser/django/trunk/django/db/models/fields/related.py#L1059

                    # If this is an m2m-intermediate to self,
                    # the first foreign key you find will be
                    # the source column. Keep searching for
                    # the second foreign key.

So you have that

所以你有

s1 = set(my_post.relevancy.all())
s2 = set(t.tto for t in my_post.tfrom.select_related('tto'))
assert not s1.symmetric_difference(s2)

In simpler SQL, the descriptor p.relevancy is getting all the Post instances where the ID is in

在更简单的SQL中,描述符p.relevancy将获取ID所在的所有Post实例

select traffic.tto_id where traffic.tfrom_id = my_post_id

#1


1  

This depends on the intermediate model definition. The firs FK of the intermediate model is what defines the 'principal' access, so

这取决于中间模型定义。中间模型的第一个FK定义了“主要”访问,因此

a_post.relevancy.all()

is the set of all the Post instances with a traffic from a_post to them .

是从a_post到它们的流量的所有Post实例的集合。

a_post.relevant.all()

is the set of all the Post instances with a traffic from them to a_post.

是所有Post实例的集合,具有从它们到a_post的流量。

You should really think about your naming conventions, it's pretty confusing. Something in the line of

你应该考虑一下你的命名约定,这很令人困惑。有点像

links = models.ManyToManyField("self",
             through="Traffic",
             symmetrical=False,
             related_name="referrers",
             blank=True)

The key is in the django sorce code:

关键在于django sorce代码:

https://code.djangoproject.com/browser/django/trunk/django/db/models/fields/related.py#L1059

                    # If this is an m2m-intermediate to self,
                    # the first foreign key you find will be
                    # the source column. Keep searching for
                    # the second foreign key.

So you have that

所以你有

s1 = set(my_post.relevancy.all())
s2 = set(t.tto for t in my_post.tfrom.select_related('tto'))
assert not s1.symmetric_difference(s2)

In simpler SQL, the descriptor p.relevancy is getting all the Post instances where the ID is in

在更简单的SQL中,描述符p.relevancy将获取ID所在的所有Post实例

select traffic.tto_id where traffic.tfrom_id = my_post_id