django以每周一次的每日时尚方式对查询元素进行排序 [英] django sort the query elements in a weekly monthly daily fashion

查看:133
本文介绍了django以每周一次的每日时尚方式对查询元素进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个项目中获得了一个人的列表和他们从api获取的测试。我希望用户有一个选项,可以看到在城市中发生的测试次数有三个选项 - 每天/每周/每月。



models.py

  class City(models.Model):
city_name = models.CharField(max_length = 100,default ='',blank = False)

类Person(models.Model):
title = models.CharField(max_length = 3, mr,blank = False)
name = models.CharField(max_length = 50,default ='',blank = False)
address = models.CharField(max_length = 200,default = blank = False)
city = models.ForeignKey(City)
class Test(models.Model):
person = models.ForeignKey(Person)
date = models.DateTimeField auto_now_add = True)
test_name = models.CharField(max_length = 200,default ='',blank = False)
subject = models.CharField(max_length = 100,default ='')

每个城市的人的json文件都是这样生成的ef =http://pastebin.com/pYLBjrjh =nofollow> http://pastebin.com/pYLBjrjh



相关 views.py

  def personlist(request,id):
data = requests.get('http://127.0.0.1:8000/app/cities/'+ id +'/ persons /')。json()
context = RequestContext(request,{
'person':data ['results'],'count':data ['count'],
})

return render_to_response('template.html',context)

这将生成在该城市进行测试的名称列表。我想知道是否有办法让用户选择时间段(每天/每周/每月),只能查看在该时间段内进行测试的名称。

解决方案

为了通过Person Model访问测试,您可以定义自己的 related_name ,例如 tests 或者只是使用默认提供django test_set

  class Test(models.Model):
person = models.ForeignKey(Person,related_name ='tests')
date = models.DateTimeField(auto_now_add = True)
test_name = models.CharField(max_length = 200,default ='',blank = False)
subject = models.CharField(max_length = 100,default ='')

related_name定义一个反向关系,以便您可以执行以下查询:

  p erson erson erson erson erson erson erson(((((((((((((((((((((()())))))))))))(( date__month = month)#month是一个整数

但星期没有默认支持,但您仍然可以使用范围功能:

  import datetime 
start_date = datetime.date(2015,12,16)
end_date = start_date + datetime.timedelta(days = 7)
person_1.tests.filter(date__range =(start_date,end_date))

示例:



views.py:

  def get_current_month():
now = datetime.now()
return now.strftime(%m)

def get_current_year():
now = datetime.now()
返回。$%$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。通用电器t('month',get_current_month())#如果没有月份将当前作为默认值
year = request.GET.get('month',get_current_year())#如果没有年份将当前作为默认值
test_list = person.tests.filter(date__month = int(month),date__year = int(year))
context = {'test_list':test_list}
return render_to_response(request,'template.html ',context)

urls.py: / strong>

  url(r'^ app / persons /(?P< id& \\ d +)/ tests /',tests_by_month),

使用以下url

  app / persons /(?P< id> \d +)/ tests /?month = 12&年= 2014 

如果您没有月份发送: app / persons /(? P< id> \d +)/ tests / 您将获得当前月份和当前年度的测试


I am getting a list of people and the tests they have taken from an api in the same project. I would like the user to have an option to see the number of tests that have taken place in a city with three options - daily/weekly/monthly.

models.py

class City(models.Model):
    city_name=models.CharField(max_length=100,default='',blank=False)

class Person(models.Model):
    title = models.CharField(max_length=3,default="mr",blank=False)
    name = models.CharField(max_length=50,default='',blank=False)
    address = models.CharField(max_length=200,default='',blank=False)
    city = models.ForeignKey(City)
class Test(models.Model):
    person = models.ForeignKey(Person)
    date = models.DateTimeField(auto_now_add=True)
    test_name = models.CharField(max_length=200,default='',blank=False)
    subject = models.CharField(max_length=100,default='')

The json file for people in each city is generated like this http://pastebin.com/pYLBjrjh

related views.py

def personlist(request, id):
data = requests.get('http://127.0.0.1:8000/app/cities/' + id + '/persons/').json()
context = RequestContext(request, {
    'persons': data['results'],'count': data['count'],
})

return render_to_response('template.html', context)

This generates the list of names in that city that have taken the test. I want to know if there is a way to let the user select the time period.(daily/weekly/monthly) and only view the names that have taken the test in that time period.

解决方案

In order to have access to test through Person Model you can define your own related_name such as tests or just use the default provide by django test_set:

class Test(models.Model):
    person = models.ForeignKey(Person, related_name='tests')
    date = models.DateTimeField(auto_now_add=True)
    test_name = models.CharField(max_length=200,default='',blank=False)
    subject = models.CharField(max_length=100,default='')

related_name define a reverse relation so you can do the following query:

  person_1.tests.all()
  person_1.tests.filter(date=date)
  person_1.tests.filter(date__day=day) # day is an integer
  person_1.tests.filter(date__month=month) # month is an integer

but there is no default support for week, but you still can do it using range feature:

import datetime
start_date = datetime.date(2015, 12, 16)
end_date = start_date + datetime.timedelta(days=7)
person_1.tests.filter(date__range=(start_date, end_date))

Example :

in views.py:

def get_current_month():
    now = datetime.now()
    return now.strftime("%m")

def get_current_year():
    now = datetime.now()
    return now.strftime("%y")

def tests_by_month(request, id):
    person = Person.objects.get(id=id)
    month = request.GET.get('month', get_current_month()) # if no month take the current as default
    year = request.GET.get('month', get_current_year()) # if no year take the current as default
    test_list = person.tests.filter(date__month=int(month), date__year=int(year))
    context = {'test_list': test_list}
    return render_to_response(request, 'template.html', context)

in urls.py :

url(r'^app/persons/(?P<id>\d+)/tests/', tests_by_month),

and you use the following url

app/persons/(?P<id>\d+)/tests/?month=12&year=2014

if you send without month: app/persons/(?P<id>\d+)/tests/ you will get the tests of the current month and current year

这篇关于django以每周一次的每日时尚方式对查询元素进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆