[Fixed]-How to filter a QuerySet with relation fields compared to a (dynamic) list

1👍

thanks to schillingt, who pushed me into the right direction i came up with a solution.

since it is not possible to use model methods for filtering a QuerySet i generate a list of allowed ids during execution outside the filter function by relating to the RequestDetail model method using a second for loop. of course this can also be done using a list comprehension or something like that:

def get_queryset(self):
    queryset = Requests.objects.order_by('-ps_date_add')
    request_ids = []
    for request in queryset:
        for detail in request.details.all():
            if detail.get_category_name() in self.get_categories_enabled():
                request_ids.append(request.id)
    q = q.filter(id__in=request_ids)
    return q

this may not be the best solution in terms of efficiency if it comes to large amounts of data.

👤jnshbr

Leave a comment