class Model_A(models.Model):
pass
class Model_B(models.Model):
pass
class Model_C(models.Model):
model_a = models.ForeignKey(Model_A)
model_b = models.ManyToManyField(Model_B)
object_Model_A._meta.get_all_related_objects()
This should give access to all instances of Model_C that has a FK to
object_Model_A.
But, I'm wondering if django provides some generic mechanism to do the
invers relation? it is,given a Model_C object get all related Model_A
and Model_B objects.
Many Thanks!!
--
Marc
But, I'm wondering if django provides some generic mechanism to do the
invers relation? it is,given a Model_C object get all related Model_A
and Model_B objects.
Hi Venkatraman, with generic mechanism I wanted to say that the method
works for all models, maybe "dynamic mechanism" should be a better
name.
Anyway I write a method that does what i'm looking for.
get_related_FK(obj):
related_objects = []
for field in obj._meta.fields:
if field.__class__ is django.db.models.fields.related.ForeignKey:
related_objects.append(getattr(obj, field.name))
return related_objects
I just wondering if django ships with something like that, just for
avoid reinvent the wheel.
--
Marc