I’m trying to retrieve the Manager (or Model) for a Django foreign key. This should be straightforward but I can’t seem to find the right attribtues.
class ModelA(models.Model):
pass
class ModelB(models.Model):
thing = models.ForeignKey(ModelA)
Let’s say I have the variable modelBInstance and the string “thing” and I want to get ModelA. I’ve followed (exhaustively, I think) the obvious looking attributes of each object using getattr. Either I’m missing something or it’s not possible. And I don’t think it’s not possible.
Couple of things:
1. Model instances don’t have managers, Model’s do.
2. To get the manager, of the foreign key you will have to first reference its class and then reference it manager.
type(getattr(modelBInstance,’thing’)).objects would give you access to the manager.