python - @property 在 Django 中的 models.py 中有两个函数

标签 python django django-models

我的模型由以下内容组成:

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')

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

@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获取当天的“真实”值。

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

最佳答案

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

例如,我们可以将支票写为:

Employee(models.Model):

    # ...

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

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

    <b>@property</b>
    def day_st(<b>self</b>):
        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]

    <b>@property</b>
    def day_end(<b>self</b>):
        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 模型中声明三个 @property,我们就完成了。

Note: since it appears that you are using integers in your Employee model for monday_st, monday_end, etc., you should use IntegerField fields [Django-doc]. By using an IntegerField, your database can help guard the type integrity of your models, and furthermore you do not have to do a lot of manual casting yourself.

关于python - @property 在 Django 中的 models.py 中有两个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56084423/

相关文章:

django - 保存模型 int() 参数必须是字符串或数字,而不是 'tuple'

python - 当转换为 Protobuf/C++ float 时,Python float 何时会失去精度?

python - Django 不显示带有 debug True 的图像

python - 创建 super 用户 django.db.utils.IntegrityError : NOT NULL constraint failed

python dateutilrelativedelta值超出范围错误

python - 引发 RuntimeError ('You need to use the eventlet server. '

python - 使用Python中的请求迭代向API发送请求

python - 使用 NLTK 简化法语 POS 标签集

javascript - Django 客户端验证小数字段限制

python - 基于 Django 模型内部方法的计算字段