Django Foreign Key: get related model?

2020-05-25 07:39发布

问题:

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.

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


回答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.