python - Can I filter users to require a matching entry on every date in a given range? -


is possible filter queryset in such way retrieve users don't have matching object every date in range?

for example, have shift model includes foreignkey field pointing employee object , date_requested datefield indicating date of shift. i've created following query grabbing user doesn't have shift between, say, 2015-07-19 , 2015-07-25:

employee.objects.all().exclude(     shift__date_requested__gte=datetime.date(2015, 7, 19),     shift__date_requested__lte=datetime.date(2015, 7, 25)) 

unfortunately queryset doesn't catch user has specified shifts 07/19 through 07/24, not 1 07/25. possible further refine query say, "the number of shifts associated user between 2015-07-19 , 2015-07-25 (inclusive) must equal number of days within time frame?

edit: here's code ran according 1 of answers below:

first, dataset showing employee #10 has shift on every day between 7/19 7/25. should lead them being filtered out:

dataset

and filtering code

e = employee.objects.all() print('all employees:\n{0}'.format(e)) day in range(19, 26):     print('filtering day {0}'.format(day))     e.filter(shift__date_requested=datetime.date(2015, 7, day))  print('filtered employees:\n{0}'.format(e)) 

and resulting output:

all employees: [<employee: 12: ereiner@test.com ()>, <employee: 10: alufson@test.com ()>, <employee: 11: tjones@test.com ()>, <employee: 14: wmcgee@test.com ()>, <employee: 13: wcarver@test.com ()>] filtering day 19 filtering day 20 filtering day 21 filtering day 22 filtering day 23 filtering day 24 filtering day 25 filtered employees: [<employee: 12: ereiner@test.com ()>, <employee: 10: alufson@test.com ()>, <employee: 11: tjones@test.com ()>, <employee: 14: wmcgee@test.com ()>, <employee: 13: wcarver@test.com ()>] 

as can see, employee 10 isn't being filtered out. tried swapping out filter() exclude() saw same result

if you're looking queryset of employees not have shift each , every 1 of given dates:

employees = employee.objects.all() day in range(19, 26):     employees = employees.filter(shift__date_requested=datetime.date(2015, 7, day)) 

note chaining filters does not hit database multiple times.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -