1
You chained them in the wrong order, do it this way:
Project.objects.select_related('funders').get(id=project_id)
and
Project.objects.select_related('funders').filter(id__in=[‘list-of-ids])
You have to call select_related
and prefetch_related
on the Manager object (Project.objects
).
1
select_related()
is a queryset method. The documentation on querysets has two sections of methods: methods that return a new queryset, and methods that do not return a queryset. get()
is in the second section, so you can’t chain any other queryset methods after it.
One other thing: prefetch_related()
runs an extra query for each model. Since you’re only fetching a single project, project.funders.all()
will run exactly 1 query to fetch all related organizations regardless of your use of prefetch_related()
. prefetch_related
becomes useful when you need the related organizations for multiple projects.
- [Answered ]-Django server details
- [Answered ]-How to set default styles for Markdown in email
- [Answered ]-How to get list post data in django forms?
- [Answered ]-Combining two columns into one using django_tables2
0
Since funder is m2m you cannot use select_related, you have to us prefetch instead. select_related only work on foreign key and one to one relation
Project.objects.prefetch_related('funders').filter(id__in=[id1, id2])