I have the following models:
class App(models.Model):
name = models.CharField(max_length=20)
...
class Request(models.Model):
...
app = models.ForeignKey(App)
...
When trying the following:
a = App.objects.all().prefetch_related('request_set')
for r in a.requests:
print r
It’s giving:
AttributeError: 'QuerySet' object has no attribute 'requests'
Why is that?
You’re using the wrong attribute to access the requests on your App – you need to use
a.request_set(or setrelated_namein yourForeignKeydefinition to be ‘requests’).