未加星标

Date lookups in Django

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

A while ago I tweeted out something that I've wanted to see in Django for a very long time, yet have never really taken the time to investigate or implement it:

I wish #django had this: M.objects.filter(datetimefield__date= http://t.co/MVFXsN4Ivk (2015, 6, 29)) Has that ever been attempted?

― Brad Montgomery (@bkmontgomery) June 29, 2015

Django's ORM has a very rich set of field lookups , but at present, it doesn't support an exact date lookup. At least not with the syntax I would expect. Luckily, that tweet got some very handy responses, so let's do a bit of exploring.

Assume we have the following model. For the now, the only field we care about is the created field.

from django.db import models
class Item(models.Model):
title = models.CharField(max_length=128)
created = models.DateTimeField(auto_now_add=True)

Again, what I wish we could do is an exact date lookup using a __date lookup. If you try that:

from datetime import date
Item.objects.filter(created__date=date(2015, 10, 1))

Well, you get an exception.

FieldError: Unsupported lookup 'date' for AutoCreatedField or join on the field not permitted.

Fear not, there are a handful of ways to acheive the same results. One way is to use the date-based lookups that django already provides. Such as __day , __month , and __year .

dt = date(2015, 10, 1)
Item.objects.filter(
created__day=dt.day,
created__month=dt.month,
created__year=dt.year
)

That's a technique I've used quite a lot in the past. It's fairly verbose, yet it's pretty readable. Another technique is to use the __range lookup. You can use a datetime object's replace method to set minimum and maximum values for the time-related attributes (and the docs for datetime objects list these values).

# Create a datetime object spanning a full day
dt = datetime.now()
start = dt.replace(hour=0, minute=0, second=0, microsecond=0)
end = dt.replace(hour=23, minute=59, second=59, microsecond=999999)
# Query for objects that fall within that day.
Item.objects.filter(created__range=(start, end))

One neat suggestion came from Jason Myers notes that you can use the __contains lookup with dates:

Item.objects.filter(created__contains=date(2015, 10, 1))

Another from Josh Ourisman takes advantage of the fact that dates are really just strings (at least in postgres):

Item.objects.filter(created__startswith='2015-10-01')

Joshua Ginsberg suggested using a custom database function prior to filtering. So, in the spirit of learning new things available in Django 1.8, I did just that. With a Func() expression .

In PostgreSQL, you could run the following query to view the date store on an Item:

select created from items limit 1;
> 2015-10-01 12:37:23.620442+00

It turns out, PostgreSQL has a handy date function that gives us just the date part of a datetime string.

select date(created) from items limit 1;
> 2015-10-01

Func() expressions (using in conjunction with F() expressions and the aggregate method will let us call postgres's date function from python!

from django.db.models import Func, F
# Note, this is PostgreSQL-specific.
# Build a queryset annotated with the date portion of the `created` datetime.
queryset = Item.objects.annotate(
created_date=Func(F('created'), function='DATE')
)
# Now, we can query agains that annotation:
items = queryset.filter(created_date=date(2015, 10, 1)) # What we want!

Pretty Cool!

So there you have a number of ways to do date-based lookups in Django. These will probably get you where you want to go most of the time.

But wait! There's more!It turns out, you can build your own lookups. The Lookup API reference was introduced in Django 1.7, yet I've not dabbled with it. Stay Tuned, because in my next post, I'll see just how hard it is to implement that __date lookup (if nothing else gets in my way over the next few days).

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: DjangoPostgreSQLSQLF#
分页:12
转载请注明
本文标题:Date lookups in Django
本站链接:http://www.codesec.net/view/483897.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(30)