0

我有以下表格,我想用我准备的自动完成小部件覆盖 manytomanyfield。

class AutoCompleteForeignKeyWidget(forms.TextInput):
    input_type = 'text'


class FooForm(forms.ModelForm):

    regions = forms.CharField(widget=AutoCompleteForeignKeyWidget(attrs={
        'class': 'regions', 'placeholder': 'Type Region',
        'autocomplete': 'off'
    }), required=True)

    class Meta:
        model = Hotel
        exclude = ('regions')

但这是不成功的。我得到一个完整性错误DETAIL: Key (region_id)=(0) is not present in table "destinations_region".有没有办法覆盖 manytomanyfield

4

1 回答 1

0

如果我错了,请纠正我,但这不起作用,因为浏览器会将输入内容作为表单字段值发送。

这意味着 django 将收到 region = 'Some region name'。但是 Django ManyToManyField 旨在使用主键列表,例如[1,2,3]1第一个选定区域的 PK 在哪里,等等。

我建议您使用自动完成应用程序,这样会更容易。

于 2013-05-06T10:48:05.527 回答