Django Crispy Forms:禁止创建没有 'fields' 属性或 'exclude' 属性的 ModelForm;

Django Crispy Forms: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited;

在 ModelForm 上使用 Django Crispy Forms 时,我不断收到错误消息:
Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited;

根据 this 网站,我必须这样制作表格:

class HotelImageForm(ModelForm):
    helper = FormHelper()
    helper.form_tag = False
    helper.layout = Layout(
        "image",
        "alt_name"
    )

    class Meta:
        model = HotelImage

当我这样做时,我得到了我提到的错误。

然而,当我将字段放在那里时,脆皮表格似乎并没有太大作用。

class HotelImageForm(ModelForm):
    helper = FormHelper()
    helper.form_tag = False
    helper.layout = Layout(
        "image",
        "alt_name"
    )

    class Meta:
        model = HotelImage
        fields = ('image', 'alt_name')

我也添加了 def __init__ 但是也没有用

class HotelImageForm(ModelForm):
    def __init__(self,  *args, **kwargs):
        super(HotelImageForm, self).__init__(*args, **kwargs)
        helper = FormHelper()
        helper.form_tag = False
        helper.layout = Layout(
            "image",
            "alt_name"
        )

    class Meta:
        model = HotelImage

这是视图中的代码:

def hotel_registration(request):
    if request.method == 'POST':
        hotel_form = HotelForm(request.POST, instance=Hotel())
        hotel_image_form = HotelImageForm(request.POST, instance=HotelImage())
        if hotel_form.is_valid():
            new_hotel = hotel_form.save()
            hotel_image = hotel_image_form.save()
            hotel_image.hotel = new_hotel
            employee.save()
            employee.hotel.add(new_hotel)
            return HttpResponseRedirect(reverse(hotel_registered))
    else:
        hotel_form = HotelForm(instance=Hotel())
        hotel_image_form = HotelImageForm(instance=HotelImage())

    context = {
        'hotel_form': hotel_form,
        'hotel_image_form': hotel_image_form,
    }
    context.update(csrf(request))

    return render_to_response('hotel/hotel-registration-form.html', context)

模板中的代码:

    <div class="col-md-5">
        {{ hotel_form|crispy }}
        {% crispy hotel_image_form hotel_image_form.helper %}
        <input type="submit" class="btn btn-primary btn-lg margin-bottom-20" value="Registreren">
    </div>

hotel_form 目前不在表单代码中使用 Crispy 表单。

我希望有人能告诉我我在这里缺少什么。

谢谢。

更新:

你试过了吗fields = '__all__'

尝试将助手的 render_unmentioned_fields 属性设置为 true 检查文档 here。它看起来像这样:helper.render_unmentioned_fields=True