anjaneyulubatta505
anjaneyulubatta505

Reputation: 11665

Django datetime month and year filtering not working

I am trying to filter with date and month in a query but unable to filter with month. I checked my code but I am unable to find the error in my code please help me .
am I missing something in below code ?

class CustomerVisits(AdminOnlyMixin, ListView):
    model = Visit
    template_name = "affiliates/admin/visits/list.html"
    paginate_by = 10
    context_object_name = "visits"
    def get_affiliate_user(self):
        affiliate = Affiliate.objects.get(affiliate_user=self.request.user)
        return affiliate

def get_queryset(self):
    object_list = self.model.objects.all()
    email = self.request.GET.get('email') # [email protected]
    date = self.request.GET.get('monthYear')# January 2016

    if email:             
        object_list = object_list.filter(affiliate_user__affiliate_user__email=email)
    if date:
        month, year = date.split()
        month_num = {
                'Jan' : 1,
                'Feb' : 2,
                'Mar' : 3,
                'Apr' : 4,
                'May' : 5,
                'Jun' : 6,
                'Jul' : 7,
                'Aug' : 8,
                'Sep' : 9, 
                'Oct' : 10,
                'Nov' : 11,
                'Dec' : 12
        }[month[:3]]
        object_list = object_list.filter(visit_time__year=year,visit_time__month=month_num)
        print object_list
    return object_list

Upvotes: 1

Views: 1490

Answers (1)

varnothing
varnothing

Reputation: 1299

you have to install time zone definition to your database to be able to use this filter when USE_TZ = True.

you can install it using mysql_tzinfo_to_sql

sudo mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -u root mysql

Here is my detailed answer

Upvotes: 1

Related Questions