以编程方式识别 django 外键链接

发布于 2025-01-02 20:44:54 字数 595 浏览 0 评论 0原文

类似于我在此处提出的问题,如果我想列出所有模型中的外键关系,有没有办法自动检测这些关系(向前和向后)?

具体来说,如果模型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风启觞 2025-01-09 20:44:54

这是您正在寻找的:

yourModel._meta.get_all_related_objects()

示例(已编辑):

class Alumne(models.Model):
    id_alumne = models.AutoField(primary_key=True)
    grup = models.ForeignKey(Grup, db_column='id_grup')
    nom_alumne = models.CharField("Nom",max_length=240)
    cognom1alumne = models.CharField("Cognom1",max_length=240)
    cognom2alumne = models.CharField("Cognom2",max_length=240, blank=True)
    ...

class Expulsio(models.Model):                             <---!
    alumne = models.ForeignKey(Alumne, db_column='id_alumne')
    dia_expulsio = models.DateField(blank=True)
    ...


>>> from alumnes.models import Alumne as A
>>> for x in A._meta.get_all_related_objects():
...     print x.name
... 
horaris:alumneexclosdelhorari
presencia:controlassitencia
incidencies:entrevista
incidencies:expulsio                                      <---!
incidencies:incidencia
incidencies:incidenciadaula
seguimentTutorial:seguimenttutorial

This is you are looking for:

yourModel._meta.get_all_related_objects()

Sample (Edited):

class Alumne(models.Model):
    id_alumne = models.AutoField(primary_key=True)
    grup = models.ForeignKey(Grup, db_column='id_grup')
    nom_alumne = models.CharField("Nom",max_length=240)
    cognom1alumne = models.CharField("Cognom1",max_length=240)
    cognom2alumne = models.CharField("Cognom2",max_length=240, blank=True)
    ...

class Expulsio(models.Model):                             <---!
    alumne = models.ForeignKey(Alumne, db_column='id_alumne')
    dia_expulsio = models.DateField(blank=True)
    ...


>>> from alumnes.models import Alumne as A
>>> for x in A._meta.get_all_related_objects():
...     print x.name
... 
horaris:alumneexclosdelhorari
presencia:controlassitencia
incidencies:entrevista
incidencies:expulsio                                      <---!
incidencies:incidencia
incidencies:incidenciadaula
seguimentTutorial:seguimenttutorial
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文