我创建了一个带有django-selectable小部件的ModelForm:
class SomeRequestForm(forms.ModelForm):
created_by = selectable.AutoCompleteSelectMultipleField(lookup_class=SomeGroupUser,
label='',
required=True,
widget=selectable.AutoCompleteSelectMultipleWidget)
我的查询:
class SomeGroupUser(ModelLookup):
model = User
search_fields = (
'username__icontains',
'first_name__icontains',
'last_name__icontains',
)
filters = {'is_active': True, }
def get_item_label(self, item):
# Display for choice listings
return u"%s, gebruikersnaam: %s" % (item.get_full_name(), item.username)
在我看来:
if request.method == "POST":
some_request_form = SomeRequestForm(request.POST, current_user=request.user,
, prefix="peer", )
else:
some_request_form = SomeRequestForm(current_user=request.user,prefix="peer")
这给出了以下错误:
Cannot assign "[<User: Test Test>]": "Some.created_by" must be a "User" instance.
然后我尝试循环request.POST列表,并为每个用户创建一个表单并获取用户对象:
if request.method == "POST":
post_values = request.POST.copy()
created_by_users = request.POST.getlist('peer-created_by_1')
for created_by_user in created_by_users:
created_by_user = User.objects.get(pk=5)
post_values['peer-created_by_1'] = created_by_user
peerassessment_form = PeerassessmentRequestForm(post_values, current_user=request.user, prefix="peer", )
但后来我收到了这个错误:
int() argument must be a string or a number, not 'User'
然后我尝试将post值更改为用户的pk并收到此错误:
Cannot assign "[<User: Test Test>]": "Some.created_by" must be a "User" instance.
完整的追溯: