Django Foreign Key: get related model?
django, django-models, foreign-keys, python
Solution
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.
Update for Django>=2.0 users
Syntax has changed. Use the below code to get the related model:
ModelA._meta.get_field('foo').related_model
Problem
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?