Query multiple models with one value

Posted by swoei on Stack Overflow See other posts from Stack Overflow or by swoei
Published on 2010-01-04T19:06:37Z Indexed on 2010/04/20 19:03 UTC
Read the original article Hit count: 288

Filed under:
|

I have multiple models which all have a FK to the same model. All I know is the FK how can I determine which of the models has the FK attached?

Below an example to clearify:

class ModelA(models.Model):
      title = models.CharField("title", max_length=80)

class ModelB(models.Model):
      fk = models.ForeignKey(ModelA)

class ModelC(models.Model):
      fk = models.ForeignKey(ModelA)

How can I figure out without using a try/except on each model whether B or C has the FK? (The FK can only be in one of them, for the record in this case I only added two models but in the real world app there are multiple possible x amount of models which have the FK to modelA)

© Stack Overflow or respective owner

Related posts about django-models

Related posts about query