Tuesday, November 26, 2019

eager DB fetching of nested objects

Hi,

I would like to query an object from a DB and fetch all nested objects.

For example:

class ModelA(models.Model):
    ...

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

class ModelC(models.Model):
    b = models.ForeignKey(ModelB)
    ...

I want to query C by it's ID and to eagerly fetch A and B:
ModelC.objects.filter(pk=id).select_related('b')

But how to I do it when I want to already fetch also a when I call c.b?

Thank you



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABQ7qCmxfbivT63Ud_BtA5xgMCbiLduN_WptY9sXH2y1kh5hPg%40mail.gmail.com.

No comments:

Post a Comment