Django shell get models object 函数不起作用有什么想法吗?

Django shell get models object function does not work any ideas?

我试图查看 django 文档,但找不到我要找的东西。我有一个 django 模型,在这个模型中我定义了 som 逻辑,问题是当我尝试通过 django shell 获取 recepie 时我无法获取值。我想看看 def recepie_status 是否正常工作。

我的模特:

class Recepie(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=60, null=False, blank=True, verbose_name='Recepie name')
description = models.TextField(max_length=500, null=True, blank=True, verbose_name='Description')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# slug = models.SlugField(max_length=255, verbose_name=_('Recepie Slug'), default=name)
share = models.BooleanField(null=True, blank=True)

def recepie_status(self):
    import datetime 
    import date, timedelta, datetime
    status=(date.today()-timedelta(days=15))
    if self.created_at > status:
        return "New"
    else:
        return "Old"

我已经在 django 中做到了 shell:

>>> one = Recepie.objects.get(pk=1)
>>> print (one.name) #this works
>>> from datetime import timedelta, date, datetime
>>> print (one.recepie_status())

在 django 中抛出这个错误 shell

 E:\Projekt\Fooders\fooders\recepies\models.py in recepie_status(self)
 18
 19     def recepie_status(self):
 20         status=(date.today()-timedelta(days=15))
 21         if self.created_at > status:
 22             return "New"

 ModuleNotFoundError: No module named 'date'

下一行的问题 导入日期

导入日期是错误的原因, 要导入日期,请​​执行以下操作

 from datetime import date