Assuming we know a ForeignKey field exists on one known arbitrary model that references another, how do we find the name of this relationship?
I am looking for something like:
Spam.field_relating_to(Egg)
that would return u'egg' where Spam looks something like this:
from django.db import models
class Spam(models.Model)
egg = models.ForeignKey(Egg)
Is this possible?
To follow up on Max Peterson’s answer, here is code you could use:
and then
should work.
Note that this might match one-to-one and many-to-many relationships as well, so if that’s important to check for you’ll have to change the code somewhat.
Haven’t tested it, so double check.