Django: storing multiple Prefetch () objects in a variable or method

The documentation shows how to store the Prefetch () object in a variable:

>>> prefetch = Prefetch('choice_set', queryset=voted_choices, to_attr='voted_choices')
>>> Question.objects.prefetch_related(prefetch).get().voted_choices
[<Choice: The sky>]

      

However, prefetch_related accepts many Prefetch () objects, separated by commas:

>>> Question.objects.prefetch_related(Prefetch('choice_set'), Prefetch('foo')).get().voted_choices

      

How can this Prefetch () sequence be stored in a variable, or even better in a method for reuse?

+3


source to share


1 answer


I prefer to add these Prefetch related clauses to a custom QuerySet and then access the generated lists through the model properties if they exist.

using: Post.objects.filter(...).prefetch_comments()...



class PostQuerySet(models.QuerySet):
    def prefetch_comments(self):
        inner_qs = Comment.objects.order_by('-date')
        return self.prefetch_related(Prefetch("comments", queryset=inner_qs, to_attr="comments_list"))


class Post(models.Model):
    ....
    objects = PostQuerySet.as_manager()

    @property
    def most_recent_comment(self):
        if hasattr(self, 'comments_list') and len(self.comments_list) > 0:
            return self.comments_list[0]
        return None

      

+4


source







All Articles