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?
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:(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 getModelC.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.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:
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: