【问题标题】:@property with two functions inside of models.py in Django@property 在 Django 中的 models.py 中有两个函数
【发布时间】:2019-09-28 18:22:45
【问题描述】:

我的模型包含以下内容:

class Employee(models.Model):
    #time work
    monday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
    monday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
    tuesday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
    tuesday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
    wednesday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
    wednesday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
    thursday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
    thursday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
    friday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='8')
    friday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='20')
    saturday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='0')
    saturday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='0')
    sunday_st = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='0')
    sunday_end = models.CharField(max_length=2, choices=CHOICES_DAY_WORK, default='0')

我想过滤当前正在工作的员工。所以这是他们工作的反映,在一个看起来像这样的模型中。

@property
def is_expired(self):
    actual_hour = int(datetime.datetime.now().strftime('%H'))
    if actual_hour in list(range(int(self.monday_st), int(self.monday_end))):
        return True
    return False

如果员工当前正在工作,则在我的 views.py 中返回“True”。

if instance.is_expired == True:
    'the employee is working'
else:
    'no working'

但是如果员工只在给定的一天工作,它需要信息,所以我创建了一个带有辅助功能的文件,看起来像这样。

def day_st():
    actual_day_number = datetime.datetime.today().weekday() + 1
    if actual_day_number == 1:
        return monday_st
    if actual_day_number == 2:
        return tuesday_st
    if actual_day_number == 3:
        return wednesday_st
    if actual_day_number == 4:
        return thursday_st
    if actual_day_number == 5:
        return friday_st
    if actual_day_number == 6:
        return saturday_st
    if actual_day_number == 7:
        return sunday_st

def day_end():
    actual_day_number = datetime.datetime.today().weekday() + 1
    if actual_day_number == 1:
        return monday_end
    if actual_day_number == 2:
        return tuesday_end
    if actual_day_number == 3:
        return wednesday_end
    if actual_day_number == 4:
        return thursday_end
    if actual_day_number == 5:
        return friday_st
    if actual_day_number == 6:
        return saturday_end
    if actual_day_number == 7:
        return sunday_end

但我不能在我的模型中使用它。我正在尝试类似的方法。

from .my_helped_function import day_st, day_end
    @property
    def is_expired(self):
        actual_hour = int(datetime.datetime.now().strftime('%H'))
        if actual_hour in list(range(int(self.day_st), int(self.day_end))):
            return True
        return False

这上面我写的不起作用。如何才能以最快的速度从@property 获得当天的“真”值。

在我看来,我的方式相当复杂,所以我对如何更快地做到这一点持开放态度。

【问题讨论】:

  • 只是一个旁注。如果我是你,我希望有一个包含四列的表:员工、日期/日期、开始时间和结束时间。这对我来说更有意义,更容易查询、分析等。
  • 我可以请你展示一个完整答案的例子吗?提前谢谢你。

标签: python django django-models


【解决方案1】:

通过稍微简化代码,这可能会更容易解决,也更高效。

例如我们可以把支票写成:

Employee(models.Model):

    # ...

    @property
    def is_expired(self):
        hour = datetime.datetime.now().hour
        return hour in range(int(self.day_st), int(self.day_end))

现在我们可以简单地定义两个额外的@propertys day_stday_end,用:

    @property
    def day_st(self):
        day = datetime.datetime.today().weekday()
        return (
            self.monday_st,
            self.tuesday_st,
            self.wednesday_st,
            self.thursday_st,
            self.friday_st,
            self.saturday_st,
            self.sunday_st)[day]

    @property
    def day_end(self):
        day = datetime.datetime.today().weekday()
        return (
            self.monday_end,
            self.tuesday_end,
            self.wednesday_end,
            self.thursday_end,
            self.friday_end,
            self.saturday_end,
            self.sunday_end)[day]

因此,通过在 Employee 模型中声明三个 @propertys,我们就完成了。

注意:由于您在 Employee 模型中使用 整数 来表示 monday_stmonday_end 等,因此您应该使用 @987654321 @。通过使用IntegerField,您的数据库可以帮助保护模型的类型完整性,而且您不必自己进行大量手动转换。

【讨论】:

  • 但是如果我希望该字段在用户看来是这样的呢?示例时间 9.00。在我的choices.py '('9', '9.00'),' 在这种情况下是否也可以使用整数字段?如何?用'(9, '9.00'),还是用表格覆盖?
  • @MaddieGraham:是的,例如见here。然后,您可以使用choices=[(0, '0:00'), (1, '1:00')] 作为选项,因此将“键”(您放入模型中的值)作为第一个元素,将“值”(文本表示)作为第二个元素。
【解决方案2】:

由于您是从 Model 类外部导入这些辅助函数,因此请尝试这样做:

# . . .
def is_expired(self):
    # . . .
    if actual_hour in list(range(int(day_st()), int(day_end()))):
    # . . .

【讨论】:

  • 尝试执行此操作时,他收到这样的错误:'Employee' object has no attribute 'day_st'。我用if actual_hour in list(range(int(self.day_st()), int(self.day_end()))):
  • 尝试删除self.
  • 返回:名称'friday_st'未定义(当前星期几)
猜你喜欢
  • 1970-01-01
  • 2010-11-27
  • 2023-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 2012-08-28
相关资源
最近更新 更多