I want to display a complete list of all submissions, and depending on the status of the most recent assessment for each submission, I want to display some additional information for each submission. In the system, there might be thousands of submissions and a handful of assessments for each of them.
On Thursday, June 27, 2019 at 11:02:25 PM UTC+2, Charlotte Wood wrote: > > What are you trying to do? > > On Thu, Jun 27, 2019, 2:09 PM swimmer <[email protected] <javascript:>> > wrote: > >> Dear Django community, >> >> I have a data model that looks like the following: >> >> class Submission(models.Model): >> ... >> >> class Assessment(models.Model): >> submission = models.ForeignKey( >> Submission, on_delete=models.CASCADE, related_name='assessments') >> time_stamp = models.DateTimeField() >> >> And I would like to do something simple such as computing a list of pairs >> of submissions and their most recent assessments: >> >> qs = Submission.objects.prefetch_related('assessments') >> xs = [(s, s.assessments.latest('time_stamp')) for s in qs.all()] >> >> This seems to be inefficient because the assessments table seems to be >> queried for each submission. >> I've read about prefetch_related and tried to use it as above but it >> doesn't seem to have any effect. >> I suppose this is because I try to use 'latest'? >> Is prefetch_related applicable in this case at all or do I need to resort >> to a different technique like a raw SQL query? >> >> Thanks in advance! >> >> Simon >> >> -- >> 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 [email protected] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> Visit this group at https://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/e38b499f-d54b-4f6a-874b-a95af0b778b0%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/e38b499f-d54b-4f6a-874b-a95af0b778b0%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/35a5cd29-8975-493f-9111-1f4b380c73ea%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

