[Fixed]-Django Foreign Key: get related model?


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


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.

Update for Django>=2.0 users

Syntax has changed. Use the below code to get the related model:



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.

Leave a comment