Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 290
SURYA VISWANATHAN Avatar asked Mar 17 '17 04:03

SURYA VISWANATHAN


People also ask

What is an unexpected keyword argument in Python?

Unexpected keyword argument %r in %s call. Description: Used when a function call passes a keyword argument that doesn't correspond to one of the function's parameter names.

Can a python function use both positional and keyword arguments at the same time?

An important detail to be aware of is that by default any argument used to call a function in Python can be used as both a positional and a keyword argument—just not at the same time.


1 Answers

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)
like image 119
wim Avatar answered Oct 09 '22 02:10

wim