我正在使用python3.7.3和Django 2.0.13。你知道吗
基本上我想在我的网站上显示一个表单,在这个表单上可以输入一个用户(=下面模型定义的参与者)。Django ModelForm会自动将其添加到一个Choice字段,并显示一个包含所有用户的下拉列表。我不想在下拉菜单中显示所有用户的列表,而是想要一个文本输入字段。你知道吗
代码:
第一,相关部分型号.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"]
你知道吗表单.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"]
你知道吗视图.py(如果相关的话,我甚至不认为它是“形式有效的”,是吗?)你知道吗
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字段,但是如果我输入用户名(“test”),我会得到一个错误:
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\u participant”方法,但是我在它周围包装了一个try-except结构(而且我坚持使用表单.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
可以重写
participant
字段的clean方法并从中返回用户实例。如果您使用的是选项字段,则模型表单将自动执行此操作,因为表单将具有查找实例的id。因为您正在重写字段,所以您必须在清理过程中自己找到相关实例。您可以这样为参与者字段定义clean方法。你知道吗您可以添加一个函数clean\u participant,该函数将获取此输入,然后查询数据库以查找关联的用户。 它工作,然后您可以返回参与者实例。否则您需要返回错误“此参与者不存在”
举个例子:
但是您需要使用默认的modelchoicefield并将该字段的小部件更改为textinput。你知道吗
相关问题 更多 >
编程相关推荐