Those are many models
class FacultyMember(models.Model):
# some attributes
class Publication(models.Model):
# some attributes
author = models.ManyToManyField(FacultyMember, blank=True)
class Project(models.Model):
# some attributes
researchers = models.ManyToManyField(FacultyMember, blank=True)
And I want to get FacultyMember with all related project and publication with eager loading.
I try some thing like following code
FacultyMember.objects.filter(
pk=id,
first_name=first_name,
last_name=last_name
).select_related('project_set').select_related('publication_set')
However it do not working.
How to join related field in django orm with eager loading.
Manytomany uses prefetch_related;
FacultyMember.objects.filter(
pk=id,
first_name=first_name,
last_name=last_name
).prefetch_related('project_set').prefetch_related('publication_set')
Refer here https://docs.djangoproject.com/en/1.8/ref/models/querysets/#prefetch-related
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With