How to filter in prefetch_related columns?

问题内容:

I have this code, where I combine a list of articles to their (translated) descriptions:

desc = models.ArticleDescription.objects.filter(lang__iexact=translation.get_language())
c = models.Article.objects.all().order_by('articleid').prefetch_related(
    Prefetch('articledescription_set', queryset=desc, to_attr='description_tr'))

this works fine, but how can I now filter the descriptions? Let’s say I only want the description “foo”. I am looking for something like this:

desc = models.ArticleDescription.objects.filter(lang__iexact=translation.get_language())
c = models.Article.objects.all().order_by('articleid').prefetch_related(
    Prefetch('articledescription_set', queryset=desc, to_attr='description_tr')).
    filter(description_tr__description="foo")

But that doesn’t work. How can I achieve this?

问题评论:

原文地址:

https://stackoverflow.com/questions/47756670/how-to-filter-in-prefetch-related-columns

添加评论