Django Foreign Key: get related model?

被刻印的时光 ゝ 提交于 2019-12-04 10:16:49

问题


Is it possible to get the related model of a foreign key through the foreign key field itself?

For example, if I have 3 models:

class ModelA(models.Model)
    field1 = models.CharField(max_length=10)

class ModelB(models.Model)
    field1 = models.CharField(max_length=10)

class ModelC(models.Model)
    field1 = models.CharField(max_length=10)
    field2 = models.ForeignKey(ModelA)
    field3 = models.ForeignKey(ModelB)

and I want to do:

for field in ModelC._meta.fields:
    if field.get_internal_type() == "ForeignKey":
        #get the related model for field e.g. ModelA or ModelB

Is this possible using just the models themselves rather than instances of the models?


回答1:


If ModelA has an FK field named "foo", then this is how you can get the related model:

ModelA._meta.get_field('foo').rel.to

With your code, it would look like:

for field in ModelC._meta.fields:
    if field.get_internal_type() == "ForeignKey":
        print field.rel.to

If found it out by using tab completion in the shell long ago, it still works. You might want to learn to use the shell to reverse engineer stuff like that.




回答2:


When trying to extract relations like this, I use a lot of command-line experimentation. A common pattern I use is _=starting_point.<chained_attributes>;pprint((_, dir(_))). For example:

_=ModelC;pprint((_, dir(_)))
_=ModelC.field2;pprint((_, dir(_)))
_=ModelC.field2.field;pprint((_, dir(_)))
_=ModelC.field2.field.rel;pprint((_, dir(_)))
_=ModelC.field2.field.rel.to;pprint((_, dir(_)))

(You'll need to do from pprint import pprint first, naturally.) That lets me experiment with adding / removing attributes until a find the item I want... while seeing clearly what I've got and what's available at the next level down. From this, I get ModelC.field2.field.rel.to == ModelA. The same base pattern can be used to explore reverse relationships, many-to-many relationships, starting with an instance rather than the class, etc.



来源:https://stackoverflow.com/questions/10347210/django-foreign-key-get-related-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!