以编程方式标识django多对多链接 [英] Programmatically identify django many-to-many links

查看:127
本文介绍了以编程方式标识django多对多链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动生成一个对象多对多链接的列表。

I'd like to automatically generate a list of an objects many-to-many links.

假设我有5个模型:

Model 1 has a M2M link to Model 2
Model 2 has a M2M link to Models 3 and 4
Model 4 has a M2M link to Model 5

如果用户向Model 1添加了一个对象,我想给他们选项添加另一个模型1或添加模型2

If the user adds an object to Model 1, I want to give them the option to add another Model 1 or add Model 2

如果用户将一个对象添加到模型2,我想给他们添加另一个模型2的选项,或添加一个模型1,3或4。

If the user adds an object to Model 2, I want to give them the option to add another Model 2, or add a Model 1, 3, or 4.

如果用户向Model 4添加一个对象,我想给他们添加另一个Model 4或Model 5的选项/ 2。

If the user adds an object to Model 4, I want to give them the option to add another Model 4 or a Model 5/2.

等。

有没有办法自动检测这些关系(前进和后退)?

Is there a way to detect these relationships (forward and backward) automatically?

推荐答案

您可以通过 Model1()._ meta.many_to_many 将返回所有m2m关系,包括 Generi cRelations

You can access to m2m relation fields via Model1()._meta.many_to_many which will return all m2m relations, including GenericRelations.

这篇关于以编程方式标识django多对多链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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