0

I have stored data in the table with respect to dates ie there could be more than one entry for the same month, but when i retrieve them i need to get them as combined result. So is there anyway that i could query it to retrieve the data for a month that is set the range as month in django(view).

edit:

Modifications from my prev post. I need to do it for a year and i do not have a specific start date and end date. I need to set the range for months in a year irrespective of the number of days present. my database would look like this:

date        count1     count2  
2011/12/01,   12,          3 
2011/12/03,   3,           6

Now i need to retrieve count1 for the month of december. I need a generic query not just for a particular month with say 30 or 31 or 28 days.

4
  • 1
    show us your model so that we can help Commented Apr 7, 2011 at 7:57
  • You shouldn't be asking a new question using edit. If the question was answered, mark it as answered and post a new question. Also try to be more specific in your question titles. "Querying in django" doesn't tell people that you are looking for a range query. Typing a specific question title also helps you determine if the question you are about to ask has been asked before. Commented Apr 7, 2011 at 19:06
  • @kriegar its not a different question. I just tried adding a few more details to the same . Commented Apr 8, 2011 at 4:04
  • It is a different question. First you asked how to make a query for the "data." Then you asked how to get the count. Anyways edited my answer to get what you want. Commented Apr 8, 2011 at 7:48

3 Answers 3

3

docs

Assuming you have a DateField

class MyModel(models.Model):
    my_date = models.DateField()

query:

import datetime
startdate = datetime.date(...start of some month...)
enddate = datetime.date(...end of some month...)
my_model_in_range = MyModel.objects.filter(my_date__range(startdate,enddate))

edit:

count docs

import datetime

def first_day_of_month(month, year):
    return datetime.date(year, month, 1)

def last_day_of_month(month, year):
    return datetime.date(year, month+1, d.day) - datetime.timedelta(1)

# if you want query for jan 2009: month = 1, year = 2009

first = first_day_of_month(1, 2009)
last = last_day_of_month(1, 2009)

objects_in_jan_2009 = MyModel.objects.filter(my_date__range(first, last))

count = objects_in_jan_2009.count()
Sign up to request clarification or add additional context in comments.

Comments

1

After you receive you range, query you model with the following:

YouModel.objects.filter(date__gt='initial date').filter(date__lt='final date')

date should be a field in your model. the __gt and __lt aply a filter for the field to search for values greater and lesser than the parameter passed.

1 Comment

gte and lte might be more appropriate for dates unless you want to calculate a day before and after.
0

Best way to do it is through raw sql

YourModel.objects.extra(
        select={'values':'select sum(count) as count , extract(month from mymodel_db.date) as date group by date'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.