以编程方式识别 django 外键链接但通过表省略
我之前提出了一个问题,以编程方式识别外键链接。。
我发现下面的代码提取了所有一对多链接:
yourModel._meta.get_all_related_objects()
不过,我现在发现的唯一问题是它还包含通往多对多链接的中间表。因此,如果我有以下模型,
class Model_one(models.Model):
name = models.CharField("Name", max_length=30)
people = models.ManyToManyField('Model_two', blank=True, through='Association')
代码不会返回任何内容,而是返回 Association。有没有办法指定“真正的”一对多链接或以其他方式通过表格省略?即使我必须从返回的 yourModel._meta.get_all_lated_objects() 中手动删除它?
另一种提问方式是:如何识别/隔离实际上是“直通”表的链接?
I asked a question earlier to programmatically identity foreignkey links..
I found that the below code pulls all the one-to-many links:
yourModel._meta.get_all_related_objects()
The only problem I'm finding now though is that it also includes interemdiary tables that are going to many-to-many links. So if I have the below model,
class Model_one(models.Model):
name = models.CharField("Name", max_length=30)
people = models.ManyToManyField('Model_two', blank=True, through='Association')
Instead of returning nothing, the code returns Association. Is there a way to either specify "real" one-to-many links or otherwise omit through tables? Even if I have to delete it manually from the returned yourModel._meta.get_all_related_objects()?
Another way to ask this: How can I identify/isolate a link that is actually a "through" table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以测试每个项目,看看它是否在
_meta.get_all_lated_many_to_many_objects()
中:You can test each item to see if it's in
_meta.get_all_related_many_to_many_objects()
:发现一个问题引导我正确的方向:
这将给出“通过”表的名称
Found a question that led me in the right direction:
This will give the name of the "through" tables