简体   繁体   English

TypeError:+ 不支持的操作数类型:“DateField”和“datetime.timedelta”

[英]TypeError: unsupported operand type(s) for +: 'DateField' and 'datetime.timedelta'

In my model there are start_date_time , end_date_time , period and period_type fields, where end_date_time must be equal to sum of start_date_time and timedelta.在我的 model 中有start_date_timeend_date_timeperiodperiod_type字段,其中end_date_time必须等于start_date_time和 timedelta 的总和。 And in my case timedelta's day is equal to period and period_type is one of days/months/years/.在我的情况下,timedelta 的天等于period ,而period_type是天/月/年/之一。 Here are my codes:这是我的代码:

models.py : models.py

class PeriodTypeEnum(models.IntegerChoices):
    Years = 'years'
    Months = 'months'
    Days = 'days'

class Policy(models.Model):
    policy_type = models.ForeignKey('PolicyType', on_delete=models.CASCADE)
    blank = models.OneToOneField('blank_app.Blank', on_delete=models.CASCADE)
    period = models.IntegerField()
    period_type = models.IntegerField(choices=PeriodTypeEnum.choices)
    start_date_time = models.DateField(blank=True, null=True)
    end_date_time = models.DateField(default = start_date_time + timedelta(days=1), blank=True, null=True)

PS: in end_date_time field, timedelta(days + x) - x must be equal to period . PS:在end_date_time字段中, timedelta(days + x) - x 必须等于period

Django doesn't support dependencies like that in default . Django 不支持default中的依赖项。

The easiest thing to do here is to declare这里最简单的做法是声明

end_date_time = models.DateField(blank=True, null=True)

and add a并添加一个

def clean(self):
    if not self.end_date_time:
         # (or do something with `self.period`?)
         self.end_date_time = self.start_date_time + timedelta(days=1)

to the model.到 model。 Remember that .clean() is only called by forms and such, not when you .save() , so if you want that, you'll need to override .save() too.请记住, .clean .clean()仅由 forms 等调用,而不是在您调用.save()时,因此如果需要,您也需要覆盖.save()

def save(self, **kwargs):
    self.clean()
    return super().save(**kwargs)

This code will not work, because start_date_time is not a value, but a field that will keep a value.此代码将不起作用,因为start_date_time不是一个值,而是一个将保留值的字段。

As was mentioned in previous answer, you can override clean/save methods to set value for end_date_time .正如前面的答案中提到的,您可以覆盖 clean/save 方法来设置end_date_time的值。 Another option is to use pre_save or post_save signals depending on when/how start_date_time is set.另一种选择是根据start_date_time的设置时间/方式来使用pre_savepost_save信号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:+的不支持的操作数类型:'float'和'datetime.timedelta' - TypeError: unsupported operand type(s) for +: 'float' and 'datetime.timedelta' Python 2.7 获取错误 TypeError:不支持的操作数类型 /:'datetime.timedelta' 和 'datetime.timedelta' - Python 2.7 Getting error TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and 'datetime.timedelta' TypeError: 不支持的操作数类型 -: 'datetime.time' 和 'datetime.timedelta' - TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.timedelta' TypeError: 不支持的操作数类型 -: 'str' 和 'datetime.timedelta' 在 Dash 导入时 - TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta' upon Dash import %不支持的操作数类型:“ datetime.timedelta”和“ int” - unsupported operand type(s) for %: 'datetime.timedelta' and 'int' Python 错误:+ 不受支持的操作数类型:“int”和“datetime.timedelta” - Python Error : unsupported operand type(s) for +: 'int' and 'datetime.timedelta' /:'datetime.timedelta'和'datedelta'不支持的操作数类型 - Unsupported operand type(s) for /: 'datetime.timedelta' and 'datedelta' +不支持的操作数类型:“ float”和“ datetime.timedelta””,“出现在索引5”) - unsupported operand type(s) for +: 'float' and 'datetime.timedelta'", 'occurred at index 5') 例外是:-:'str'和'datetime.timedelta'不受支持的操作数类型 - Exception was: unsupported operand type(s) for -: 'str' and 'datetime.timedelta' + 不支持的操作数类型:“int”和“datetime.timedelta” - Unsupported operand type(s) for +: 'int' and 'datetime.timedelta'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM