【发布时间】:2021-05-01 07:51:30
【问题描述】:
基本问题是我不能使用key-value-argument
field_variable_name__isnull=True/False
或
field_variable_name=None
当field_variable_name 包含空格或连字符时(例如“这是我的字段变量名”或“这是另一个”)。
我查找了how to filter a models.object-list with 'varname__isnull=True',但没有找到解决方案,因为给出了包含空格字符的名称:
在我的views.py 中,我有以下功能:
def currenttodos(request):
todos = Todo.objects.filter(user=request.user, time_completed__isnull=True)
return render(request, 'todo/currenttodos.html', {'todos': todos})
models.py 文件中的模型Todo 包含以下字段:
time_completed = models.DateTimeField(null=True, blank=True)
这种方式可以工作,但实际上我想以不同的方式调用 time_completed - 字段,例如 "Completion time" 使用 name-参数:
time_completed = models.DateTimeField(null=True, blank=True, name="Completion time")
现在,问题是我不能使用__isnull - 参数,因为名称包含空格。
以下所有示例均无效:
todos = Todo.objects.filter(user=request.user, Completion time__isnull=True)
todos = Todo.objects.filter(user=request.user, "Completion time"__isnull=True)
todos = Todo.objects.filter(user=request.user, "Completion time"=None)
etc.
我怎样才能使包含空格或连字符的名称起作用?
【问题讨论】:
-
使用“完成时间”作为名称的目的是什么,总有你可以使用的标签
-
我应该在哪里使用
label-参数? Django 模型字段不包含此字段(刚刚收到错误got an unexpected keyword argument 'label')。
标签: django django-models django-views django-forms