Python development [Django]: Time Processing, pythondjango

Source: Internet
Author: User

Python development [Django]: Time Processing, pythondjango
Time Format

The time format in the database (02:10:44. 308638), formatted as the desired time (such as the year, month, and day). Next we will record how to process the time.

1. Time Format Group

Class Article (models. model): nid = models. bigAutoField (primary_key = True) title = models. charField (verbose_name = 'Article title', max_length = 128) summary = models. charField (verbose_name = 'Article introduction', max_length = 255) read_count = models. integerField (default = 0) comment_count = models. integerField (default = 0) up_count = models. integerField (default = 0) down_count = models. integerField (default = 0) create_time = models. dateTimeField (verbose_name = 'creation time', auto_now_add = True) blog = models. foreignKey (verbose_name = 'blog ', to = 'blog', to_field = 'nid') category = models. foreignKey (verbose_name = 'Article type', to = 'category ', to_field = 'nid', null = True) type_choices = [(1, "Python"), (2, "Linux"), (3, "OpenStack"), (4, "GoLang"),] article_type_id = models. integerField (choices = type_choices, default = None) tags = models. manyToManyField (to = "Tag", through = 'article2tag', through_fields = ('Article', 'tag '),)
Models. py DateTimeField type

Processing functions:

Def home (request, site): "" blogger's personal homepage: param request: param site: blogger's website suffix such as: http://xxx.com/wupeiqi.html: return: "# generate three fields: nid, num, and ctime. The date is grouped by % Y-% m. date_list = models. article. objects. raw (# raw executes native SQL statement 'select nid, count (nid) as num, strftime ("% Y-% m", create_time) as ctime from repository_article group by strftime ("% Y-% m", create_time) ') # date_format (create_time, "% Y-% m") # The above method is sqllist, mysql can replace strftime with date_format # date_list to extract data. for item in date_list: print (item. nid, item. num, item. ctime) #4 1 2017-01 #13 8 2017-02 #7 1 2017-03

Grouping the date format by year and month, and counting the number of data contained in each group

2. Search and match based on the time format

Def filter (request, site, condition, val): --- snip --- elif condition = 'date': # mysql execution method # article_list = models. article. objects. filter (blog = blog ). extra (# where = ['date _ format (create_time, "% Y-% m") = % s'], params = [val,]). all () # extra construct additional query conditions or mappings, such as: subquery article_list = models. article. objects. filter (blog = blog ). extra (# val passed in date parameters such as: 2017-02 where = ['strftime ("% Y-% m", create_time) = % s'], params = [val,]). all () # python two % represents a % character var corresponding to % s # The preceding statement is equivalent to select * from article where strftime ("% Y-% m", create_time) = 2017-02

In the Article data table, retrieve all matched data objects in the time format of 2017-02.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.