【问题标题】:How to replace Choices field in Django ModelForm with TextInput如何用 TextInput 替换 Django ModelForm 中的 Choices 字段
【发布时间】:2019-09-17 11:59:54
【问题描述】:

我正在使用 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 字段,但如果我输入用户名(“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_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

【问题讨论】:

    标签: python django django-models django-forms


    【解决方案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)
    

    【讨论】:

    • 非常感谢!我这样做并添加了一个 try/catch(请参阅更新)。 :)
    • 是的,看起来不错:)
    【解决方案2】:

    您可以添加一个函数 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,并将该字段的小部件更改为文本输入。

    【讨论】:

    • 感谢您的回答!您的回答有所帮助,尽管 get 请求的行已经引发了一个错误,因此我认为这个 if 语句似乎没有按您的意图工作。我会将我的解决方案添加到我的问题中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    相关资源
    最近更新 更多