ちょうどそれを行った、完全にテストされていないが、それはOKであるようだ。私はちょうどその期間が "dd/mm-dd/mm"として渡されると考えていました
def BirthdayFilter(queryset,name,value):
if not value:
return queryset
#considering value will be : xx/xx-xx/xx
dateBegin,dateEnd = value.split('-') #first split the Date Range
dayBegin,monthBegin = dateBegin.split('/')
dayBegin = int(dayBegin)
monthBegin = int(monthBegin)
dayEnd,monthEnd = dateEnd.split('/')
dayEnd = int(dayEnd)
monthEnd = int(monthEnd)
result = []
if monthEnd-monthBegin == 0: #dates at the same month, ok easy
if dayBegin > dayEnd:#not allowed
return result
result = queryset.filter(data_nascimento__day__range = (dayBegin,dayEnd),data_nascimento__month = monthBegin)
elif abs(monthEnd - monthBegin) > 0: #crossing month
#first the edges
edgeStart = queryset.filter(data_nascimento__day__range = (dayBegin,31), data_nascimento__month = monthBegin)
edgeEnd = queryset.filter(data_nascimento__day__range = (1,dayEnd), data_nascimento__month = monthEnd)
result = edgeStart | edgeEnd
if abs(monthEnd - monthBegin) > 1: #if it is bigger than 1, we have to add
#months in between
monthsInBetween = []
if monthBegin > monthEnd: #calendar crossing years
monthsInBetween = queryset.filter(data_nascimento__month__range = (monthBegin,12)) + Clientes.objects.filter(data_nascimento__month__range = (1,monthEnd))
else:
monthsInBetween = queryset.filter(data_nascimento__month__range = (monthBegin,monthEnd))
result = result | monthsInBetween
class UserFilter(django_filters.rest_framework.FilterSet):
birthday = django_filters.CharFilter(method = BirthdayFilter)
class Meta:
Model = User
fields = ['birthday']
あなたのモデル構造はどうか分かりません。 1つのDateFieldに何が問題なのか、それから簡単なdjangoクエリがあるのです。 –