简体   繁体   English

NameError:在Django ORM查询中未定义名称“ F”

[英]NameError: name 'F' is not defined in Django ORM query

I'm using the ipython (Python 3.7) console in PyCharm. 我在PyCharm中使用ipython(Python 3.7)控制台。 I'm trying got run a Django ORM query 我正在尝试运行Django ORM查询

Article.objects.filter((round(datetime.now(timezone.utc) - F("created_on")) / 300) * 300)

but I'm getting the following error 但出现以下错误

Traceback (most recent call last):
  File "/Users/davea/Documents/workspace/mainpage_project/venv/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-12-53618885441a>", line 1, in <module>
    Article.objects.filter((round(datetime.now(timezone.utc) - F("created_on")) / 300) * 300)
NameError: name 'F' is not defined

I thought "F" was the way to reference a field in my model in an expression but I get the error above. 我以为“ F”是在表达式中引用模型中字段的方式,但出现上述错误。 My Article model contains the field as below ... 我的文章模型包含以下字段...

class Article(models.Model):
    ...
    created_on = models.DateTimeField(default=datetime.now)

在Python控制台中,您需要导入F

from django.db.models import F

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM