我正在使用Python 3.7.3和Django 2.0.13。
基本上我想在我的网站上显示一个表单,可以在该表单上输入用户(=以下模型定义的参与者)。 Django ModelForm会将其自动添加到选择字段,并显示所有用户的下拉列表。我不想在下拉菜单中显示所有用户的列表,而是想要一个TextInput字段。
代码:
首先,来自models.py的相关部分:
class Invite(models.Model):
game = models.ForeignKey(Game, on_delete=models.CASCADE)
host = models.ForeignKey(User, on_delete=models.CASCADE, related_name = "invites_as_host")
participant = models.ForeignKey(User, on_delete=models.CASCADE, related_name = "invites_as_participant")
accepted = models.BooleanField(blank = True, default = False)
declined = models.BooleanField(blank = True, default = False)
date_created = models.DateTimeField(auto_now_add=True)
date_edited = models.DateTimeField(auto_now=True)
class Meta:
unique_together = ["game", "host", "participant"]
forms.py:
class GameInviteNewForm(forms.ModelForm):
class Meta:
model = Invite
fields = ["participant"]
我试图覆盖这样的参与者输入字段:
class GameInviteNewForm(forms.ModelForm):
participant = forms.CharField(
label=_("User to invite"),
max_length=100,
widget = forms.TextInput
)
class Meta:
model = Invite
fields = ["participant"]
views.py(如果相关;我认为它甚至没有达到“ form_valid”,对吗?)
class GameInviteNewView(LoginRequiredMixin, UserIsLeaderMixin, FormView):
form_class = GameInviteNewForm
template_name = "app/game/new_invite.html"
pk_url_kwarg = "game_id"
def get_success_url(self):
return reverse_lazy("app:game_invite", kwargs={
"game_id": self.kwargs['game_id']
})
def form_valid(self, form):
participant = form.save(commit=False)
participant = User.objects.get(username=participant.name)
host = User.objects.get(username=self.request.user.username)
game = Game.objects.get(id=self.kwargs['game_id'])
invite.participant_id = participant.id
invite.host_id = host.id
invite.game_id = game.id
invite.save()
return redirect(self.get_success_url())
这确实在网站上显示了TextInput字段,但是如果我输入用户名(“测试”),则会出现错误:
Internal Server Error: /app/game/invite/7
Traceback (most recent call last):
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/contextlib.py", line 74, in inner
return func(*args, **kwds)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/contrib/auth/mixins.py", line 52, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/contrib/auth/mixins.py", line 109, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/views/generic/base.py", line 89, in dispatch
return handler(request, *args, **kwargs)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/views/generic/edit.py", line 141, in post
if form.is_valid():
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/forms/forms.py", line 179, in is_valid
return self.is_bound and not self.errors
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/forms/forms.py", line 174, in errors
self.full_clean()
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/forms/forms.py", line 378, in full_clean
self._post_clean()
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/forms/models.py", line 396, in _post_clean
self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/forms/models.py", line 60, in construct_instance
f.save_form_data(instance, cleaned_data[f.name])
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 838, in save_form_data
setattr(instance, self.name, data)
File "/home/dremet/anaconda3/envs/django/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 197, in __set__
self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "'test'": "Invite.participant" must be a "User" instance.
该下拉菜单的每个选项的“值”属性中都包含用户ID。现在,输入一个字符串。因此,对于它不起作用并不感到惊讶,但是对于错误消息指出它必须是“用户”(而不是用户ID),我感到惊讶。 我试图覆盖“ clean()”方法并使用常规形式,但两者均未成功。应该如何正确处理?
解决方案:
正如答案中指出的那样,我确实需要一个“ clean_participant”方法,但是我在它周围包裹了一个try-except结构(并且我坚持使用forms.py更改覆盖了参与者字段):
def clean_participant(self):
participant_string = self.cleaned_data['participant']
try:
participant = User.objects.get(username=participant_string)
except User.DoesNotExist:
raise forms.ValidationError("User does not exist.")
return participant
答案 0 :(得分:1)
您可以覆盖participant
字段的clean方法,并从中返回用户实例。如果您使用选择字段,则模型表单将自动执行此操作,因为表单将具有用于查找实例的ID。因为您要覆盖领域,所以您必须在清洁过程中以某种方式自己找到相关实例。您可以通过为参与者字段定义清洁方法来做到这一点。
class GameInviteNewForm(forms.ModelForm):
class Meta:
model = Invite
fields = ["participant"]
def clean_participant(self):
# you have to return a user instance from here some how use filtering logic you want
participant = self.cleaned_data['participant']
# just an example handle exceptions and other stuff
return User.objects.get(username=participant)
答案 1 :(得分:1)
您可以添加一个函数clean_participant,该函数将获取此输入,然后查询数据库以查找关联的用户。 它可以正常工作,然后您可以返回参与者实例。否则,您需要返回错误“此参与者不存在”
这是一个例子:
def clean_participant(self):
participant = self.cleaned_data.get('participant')
q = User.objects.get(username= participant)
if q:
return q
raise forms.ValidationError("this participant doesn't exist")
但是您需要使用默认的modelchoicefield并将该字段的小部件更改为文本输入。