[英]How do i alias a composite value of 2 fields inside the values_list method of object_manager?
I am trying to create a really simple form that allows the user to select an active user from a selection list and submitting that choice to the backend.我正在尝试创建一个非常简单的表单,它允许用户从选择列表中选择一个活动用户并将该选择提交给后端。 Here is my code:
这是我的代码:
class PeerReviewColleagueSelectionForm(forms.Form):
ACTIVE_COLLEAGUES = CustomUser.objects.filter(is_active=True)\
.values_list('id', full_name=F(('first_name') + ' ' + F('last_name')))\
.order_by('full_name').annotate(Count='id')
colleague = forms.ChoiceField(label='selecteer collega', tuple=ACTIVE_COLLEAGUES)
I am trying to get a list of tuples
that can be used by the ChoiceField widget to display all the available active colleagues to choose from.我正在尝试获取一个可由 ChoiceField 小部件使用的
tuples
列表,以显示所有可用的活动同事以供选择。
I'm trying to create an alias called full_name
from the first_name and last_name fields of CustomUser.我正在尝试从 CustomUser 的 first_name 和 last_name 字段创建一个名为
full_name
的别名。 Then I want to order the results by that alias and i use annotate(count) to group by id (since i know each id is unique and i want tuples consisting of (id, full_name,)
然后我想按该别名对结果进行排序,并使用 annotate(count) 按 id 分组(因为我知道每个 id 都是唯一的,并且我想要由
(id, full_name,)
组成的元组
However when i try this it throws: TypeError: values_list() got an unexpected keyword argument 'full_name'
但是,当我尝试此操作时,它会抛出:
TypeError: values_list() got an unexpected keyword argument 'full_name'
How can i make a tuple based on the id and an alias called full_name?如何根据 id 和名为 full_name 的别名创建元组?
Unfortunatelly values_list()
doesn't accept expression keywords arguments.不幸的是
values_list()
不接受表达式关键字参数。 So you should annotate new field first:所以你应该先注释新字段:
CustomUser.objects.filter(is_active=True)
.annotate(full_name=Concat('first_name', Value(' '), 'last_name')
.values_list('id', "full_name")
.order_by('full_name')
Note that you can use Concat
database function to perform string concationation.请注意,您可以使用
Concat
数据库函数来执行字符串连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.