以编程方式识别 django 外键链接
类似于我在此处提出的问题,如果我想列出所有模型中的外键关系,有没有办法自动检测这些关系(向前和向后)?
具体来说,如果模型 1 读取
class Mdl_one(models.Model):
name = models.CharField(max_length=30)
并且模型 2 读取,
class Mdl_two(models.Model):
mdl_one = models.ForeignKey(Mdl_one)
name = models.CharField(max_length=30)
是否有一些我可以从 Mdl_one 运行的元命令(如 Model_one()._meta.one_to_many)告诉我 mdl_two 与其具有一对多外键关系?只是 mdl_one 和 mdl_two 可以连接,不一定任何两个对象实际上是?
Similar to the question I asked here, if I wanted to list all of the foreign key relationships from a model, is there a way to detect these relationships (forward and backward) automatically?
Specifically, if Model 1 reads
class Mdl_one(models.Model):
name = models.CharField(max_length=30)
and Model 2 reads
class Mdl_two(models.Model):
mdl_one = models.ForeignKey(Mdl_one)
name = models.CharField(max_length=30)
Is there some meta command I can run from Mdl_one (like Model_one()._meta.one_to_many) that tells me that mdl_two has a one-to-many foreign key relationship with it? Simply that mdl_one and mdl_two can be connected, not necessarily that any two objects actually are?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您正在寻找的:
示例(已编辑):
This is you are looking for:
Sample (Edited):