4

I want to include a month number in a queryset where the date is in a related model. This is what I tried:

 OrderItem.objects.all().annotate(order_month=Sum('order__payment_date__month'))[0].__dict__

 Join on field 'payment_date' not permitted. Did you misspell 'month' for the lookup type?

and then I tried

 OrderItem.objects.all().extra(select={'order_month': "order__payment_date__month"})

 (1054, "Unknown column 'order__payment_date__month' in 'field list'")

 OrderItem.objects.all().extra(select={'order_month': "order.payment_date"}).select_related('Order')

 (1054, "Unknown column 'order.payment_date' in 'field list'")

But this works so no problem with order.payment_date

 OrderItem.objects.all().values('id','order__payment_date').select_related('Order')

I need it in the querset result as I'm using the queryset in Geraldo. Anyone know how I can get this?

THE ANSWER was that in the extra section you need to specify what you want so the MySQL understands it. In my case adding the app in front of the model name. In this case web_order.payment_date. This worked:

OrderItem.objects.all().extra(select={'order_month': "MONTH(web_order.payment_date)"}).select_related('order')[0].__dict__

{'product_id': None, 'order_id': 1L, 'price': Decimal("1.00"),  'order_month': 7L, 'id': 1L}
5
  • Why did you do a Sum here? OrderItem.objects.all().annotate(order_month=Sum('order__payment_date__month'))[0].__dict__
    – U-DON
    Dec 7, 2011 at 11:47
  • What is a "sum of payment months" even supposed to mean? Dec 7, 2011 at 12:25
  • As month is an integer I thought I could be clever and use annotate to add this field. Sadly not!
    – PhoebeB
    Dec 7, 2011 at 14:03
  • Why do you need the month (rather than full date) in the first place? Dec 7, 2011 at 15:16
  • I want to group and sum for each month in geraldo and can't find a way of extracting the month within geraldo so want to pass it as part of the queryset.
    – PhoebeB
    Dec 7, 2011 at 15:46

2 Answers 2

10

In Django 1.10+, you can use the ExtractMonth function.

from django.db.models.functions import ExtractMonth
OrderItem.objects.all().annotate(order_month=ExtractMonth('order__payment_date'))
1

You'll need to drop down into SQL to get this done, which sadly means the solution won't be database agnostic.

This works in Postgres:

# Grab the base queryset
items = Item.objects.all()

# Annotate
extra_query = "EXTRACT(MONTH FROM relatedtable.mydatefield)"
items = items.extra(select={'mydatefield_month': extra_query}

# We have to use a select_related so that 'relatedtable' is 
# available in the SQL statement.
items = items.select_related('relatedmodel')

print items[0].mydatefield_month

For MySQL you might try:

extra_query = "MONTH(relatedtable.mydatefield)"
3
  • Hmm - not getting it working. See edit to original question.
    – PhoebeB
    Dec 7, 2011 at 14:29
  • Try changing select_related('Order') to select_related('order') Dec 7, 2011 at 23:27
  • That didn't fix it but you put me on the right road! Have put the working query in the question and I'm giving you the credit. Thanks.
    – PhoebeB
    Dec 9, 2011 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.