Search code examples
pythondjangoargumentsdjango-querysetrepeat

SyntaxError: keyword argument repeated


I have the below queryset:

site_list = SiverifyVerificationSite.objects.filter(pattern_id=int(p_id), if_target=bundle.obj.pattern.if_target).exclude(ptrf__istartswith='ptrf-mt23run1-')

It works if I give one exclude filter whereas If I include the second filter in exclude it throws (SyntaxError: keyword argument repeated). Ideally what I want is:

site_list = SiverifyVerificationSite.objects.filter(pattern_id=int(p_id), if_target=bundle.obj.pattern.if_target).exclude(ptrf__istartswith='ptrf-mt23run1-', ptrf__istartswith='ptrf-20251-') 

Is there any operators to do this. Thanks.


Solution

  • You may just chain the excludes:

    qs = qs.exclude(ptrf__istartswith='ptrf-mt23run1-')
    qs = qs.exclude(ptrf__istartswith='ptrf-20251-')
    

    It does not cause any extra queries this way - Django won't evaluate the queryset until necessary.

    An alternative is to build up the filter with Q objects.

    from django.db.models import Q
    q = Q(ptrf__istartswith='ptrf-mt23run1-') | Q(ptrf__istartswith='ptrf-20251-')
    qs = qs.exclude(q)