django conditionally filtering objects

前端 未结 5 963
野的像风
野的像风 2020-12-29 20:16

I would like to retrieve a bunch of rows from my database using a set of filters.

I was wondering if conditional filter is applicable in django. That is, \"filter if

5条回答
  •  温柔的废话
    2020-12-29 20:40

    from django.db.models import Q
    
    qs = Users.objects.filter(
                        p_id=parent_id,
                        status=True
                    ).all()
    
    if user_id>0:
        qs = qs.filter( ~Q(id=user_id) )
    

    in qs we will get the filtered results

提交回复
热议问题