【问题标题】:ModelForm for choicefield with PK instead of usernames带有 PK 而不是用户名的选择字段的 ModelForm
【发布时间】:2019-07-27 12:43:27
【问题描述】:

我有一个由 PostView 生成的表单

class HotelCreateView(LoginRequiredMixin, CreateView):
    model = Hotel
    fields = ['hotel', 'code', 'collaborateurs', 'planning' 'payday']

    def form_valid(self, form):
        form.instance.manager_hotel = self.request.user
        return super().form_valid(form)

模型 collaborateurs 是一个呈现用户名的选择字段。

我希望此字段改为呈现 PK,因此我尝试创建自己的表单但无法弄清楚。

forms.py

 from django import forms 
 from .models import Hotel

class HotelForm(forms.Form):
   collaborateurs = forms.ModelChoiceField(queryset=collaborateurs.objects.all())

谢谢

【问题讨论】:

  • 您的意思是 forms.ChoiceField 不是 forms.ColleagueChoiceField?
  • 是的,我的错。但即使这样,我的表格也没有改变
  • 我试过了,但对我的情况不起作用:class HotelCreateView(LoginRequiredMixin, CreateView): model = Hotel fields = ['hotel','code','collaborateurs', 'planning','payday'] def form_valid(self, form): form.instance.manager_hotel = self.request.user return super().form_valid(form) Hotel = form.cleaned_data['collaborateurs'].id

标签: python django django-forms django-class-based-views


【解决方案1】:

我建议您创建一个自定义小部件。

在一些“模板”文件夹中创建一个“widgets”文件夹和“pk-select.html”。

小部件/pk-select.html

<select name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
  {% for group_name, group_choices, group_index in widget.optgroups %}
    {% if group_name %}
      <optgroup label="{{ group_name }}">
    {% endif %}
    {% for option in group_choices %}
      <option value="{{ option.value|stringformat:'s' }}"{% include "django/forms/widgets/attrs.html" %}>{{ option.value }}</option>
    {% endfor %}
    {% if group_name %}
      </optgroup>
    {% endif %}
  {% endfor %}
</select>

然后,像这样修改你的“form.py”

form.py

from django.forms import ModelForm
from django.forms.widgets import Select
from .models import Hotel


class PkSelect(Select):
    template_name = 'widgets/pk-select.html'


class HotelCreateForm(ModelForm):
    class Meta:
        model = Hotel
        fields = ['hotel', 'code', 'collaborateurs', 'planning', 'payday']
        widgets = {
            'collaborateurs': PkSelect(attrs={})
        }

接下来,我希望你对“view.py”做一些改动

view.py

class HotelCreateView(LoginRequiredMixin, CreateView):
    form_class = HotelCreateForm
    template_name = 'hotel_form.html'

    def form_valid(self, form):
        form.instance.manager_hotel = self.request.user
        return super().form_valid(form)

改变的部分是“pk-select.html”中的这一行

<option value="{{ option.value|stringformat:'s' }}"{% include "django/forms/widgets/attrs.html" %}>{{ option.value }}</option>

您可以在 GitHub 页面上看到,最初,{{ option.value }}{{ widget.label }}

https://github.com/django/django/blob/master/django/forms/templates/django/forms/widgets/select_option.html

{{ widget.label }}在这种情况下显示用户名,所以我修改了这部分。

我希望这是你要找的,如果我的理解有误,请随时问我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多