Consider a simple ForeignKey relationship:
class ModelA(models.Model):
other_field = CharField()
class ModelB(models.Model):
my_field = CharField()
parent = ForeignKey(ModelA)
So I can do this:
my_fields = ModelB.objects.all().values('my_field')
Is there any way to reference other_field in the same call? I would assume something like this is possible:
all_fields = ModelB.objects.all().values('my_field', 'parent.other_field')
But apparently that’s not the case. What’s the easiest way to fetch the related field values?
If this means that the Django ORM needs to prefetch the related values resulting in a heavy query, so be it. I’m looking for the most elegant way to do this.
as per the docs you can use