gpt4 book ai didi

python - 如何在 django 中由系统(webSite)自动调用方法?

转载 作者:太空宇宙 更新时间:2023-11-03 19:01:22 25 4
gpt4 key购买 nike

我有一个名为 postState() 的方法。此方法应该检查帖子的状态。一个帖子可以有 3 种状态(新、旧、已存档),该方法以帖子作为参数。该方法根据过去的天数计算状态。

  • 30 天前的帖子是新帖子
  • 30 - 60 天的帖子已过时
  • A > 60 天的旧帖子已存档

代码如下:

#This method automatically determines the state of the post. Whether it is (New, Old, or          #   Archived)
#The method takes in one parameter which is the post itself
#the method compares the date of which the post was published in and the current date
#It then uses an algorithim to determine the difference in number of days between the current date and the published date
#Based on the amount returned, if the amount is less than 30 days, the state = "NEW", if between 30 and 60, the state = "OLD", if greater than 60, the state = "ARCHIVED"

def postState(self):
current_time = datetime.datetime.now()
p = Post.objects.get(id = self.id)

#used if the current year is greater than the year of the published post
if current_time.year > self.pub_Date.year:
#this is in case for exmaple the published month of the post is December and the current month is January
#Although the years are diff yet the diff in days may not be greater than 30
#Ex: published date: 2012, 12, 28 ----- current date: 2013, 1, 10
if current_time.month == 1 and self.pub_Date.month ==12 and (current_time.day + (31 - self.pub_Date.day)) > 30:
p.state = 'Old'
p.save()
#this is in case for exmaple the published month of the post is November and the current month is January
#Although the years are diff yet the diff in days may not be greater than 30 and less than 60
#Ex: published date: 2012, 11, 1 ----- current date: 2013, 1, 28
elif current_time.month == 1 and self.pub_Date.month ==11 and (current_time.day + (31 - self.pub_Date.day)) < 60:
p.state = 'Old'
p.save()
else:
p.state = 'Archived'
p.save()
#Used when the current year and Published year of the post are the same
if current_time.year == self.pub_Date.year:

day_diff_diff_month = current_time.day + (31 - self.pub_Date.day)
day_diff_same_month = current_time.day - self.pub_Date.day
month_diff = current_time.month - self.pub_Date.month
if month_diff >= 1:
month_diff = month_diff - 1
total_diff = (month_diff*31) + day_diff_diff_month
else:
total_diff = day_diff_same_month

if total_diff > 30 and total_diff < 60:
p.state = 'Old'
p.save()
if total_diff > 60:
p.state = 'Archived'
p.save()

我希望每当用户打开帖子页面(post.html)时运行此方法或者每当他启动网站时。

提前致谢。

最佳答案

  1. 您用于确定帖子状态的代码似乎非常复杂。我的写法如下:

    @property
    def state(self):
    """The state of the post:
    'new' if the post is younger than 30 days;
    'old' if the post is between 30 and 60 days old;
    'archived' if the post is 60 days old or older.

    """
    age = (datetime.datetime.now() - self.pub_Date).days
    if age < 30: return 'new'
    elif age < 60: return 'old'
    else: return 'archived'
  2. 如果我正确理解您的问题,您计划在每次有人查看帖子时更新帖子的状态。这将导致大量数据库更改。但既然帖子的状态可以根据 pub_Date 计算出来,为什么还要麻烦呢?为什么不在需要时计算状态,如上所示?

    您可以根据计算的日期轻松发出查询。例如,下面是一个仅选择"new"帖子的查询:

    from datetime import datetime, timedelta
    Post.objects.filter(pub_Date__gt = datetime.now() - timedelta(days=30))

    这是一个仅选择“旧”帖子的帖子:

    now = datetime.now()
    Post.objects.filter(pub_Date__range = (now - timedelta(days=60),
    now - timedelta(days=30))

    您可能还需要弄清楚如何计算数据库引擎中的状态。确切的详细信息取决于您的数据库,但例如,在 MySQL 中您可以使用 DATEDIFF function .

关于python - 如何在 django 中由系统(webSite)自动调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15978571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com