0

我正在使用通过设置中间表来处理 m2m 关系。问题是我需要显示多选而不是正常的 dorpdown 但是当我选择多个项目并保存时出现错误。

ValueError: Cannot assign "<QuerySet [<Facility: facility1>, <Facility: facility2>]>": "Property.facility" must be a "Facility" instance.

我还在 admin.TabularInline 中展示了这个模型,它允许我每行只选择一个项目,因为表格内联提供了插入多个表单的能力。

我尝试了多种解决方案,例如自定义保存和许多其他事情,以及一些我如何能够保存但随后出现的问题。我只需要显示一个带有多选小部件的表单即可执行此选择。

models.py


class Facility(models.Model):
    name = models.CharField(max_length=200)

class Property(models.Model):
    name = models.CharField(max_length=200)
    area = models.CharField(max_length=200)
    facility = models.ManyToManyField(Facility, through="PropertyFacility")

class PropertyFacility(models.Model):
    prop = models.ForeignKey(
        Property, related_name="facilities", on_delete=models.CASCADE
    )
    facility = models.ForeignKey(
        Facility, related_name="properties", on_delete=models.CASCADE
    )

admin.py


from django.contrib.admin.widgets import FilteredSelectMultiple
from django.utils.translation import ugettext_lazy as _

class PropertyFacilityForm(forms.ModelForm):
    facility = forms.ModelMultipleChoiceField(Facility.objects.all(), required=True, widget=FilteredSelectMultiple(_('ss'), False, attrs={'rows':'10'})

class PropertyFacilityInline(admin.TabularInline):
    model = Property.facility.through
    form = PropertyFacilityForm

class PropertyAdmin(TabbedModelAdmin):
    model = Property
    tab_facilities = (PropertyFacilityInline,)
    tab_property = (
        (
            "Property Details",
            {
                "fields": (
                    "name",
                    "area",
                )
            },
        ),
    )

    tabs = [
        ("Property", tab_property),
        ("Facilities", tab_facilities),
    ]

它让我像这样在管理员上显示在这里你可以看到我可以选择多项,但这里的表单应该是一个而不是多个。

我只想显示一个带有多选小部件的表单,它允许我多选并保存它,然后在 change_view 上显示选定的一个和未选择的左侧。

4

1 回答 1

0

我通过添加一些用于保存 m2m 字段的自定义逻辑解决了这个问题。

admin.py

class PropertyForm(forms.ModelForm):
    facility = forms.ModelMultipleChoiceField(Facility.objects.all(), required=False, widget=forms.CheckboxSelectMultiple)
    class Meta:
        model = Property
        fields = ["id", "name", "area", "city"]

    def save(self, commit=True):
        prop_facilities = self.cleaned_data.pop('facility')
        instance = forms.ModelForm.save(self, commit=False)
        instance.facility.clear()
        for facility in prop_facilities:
            PropertyFacility.objects.create(facility=facility, prop=instance)
        return instance


class PropertyAdmin(TabbedModelAdmin):
    model = Property
    tab_facilities = (("Facilities", {"fields": ("facility", )},),)
    tab_property = (
        (
            "Property Details",
            {
                "fields": (
                    "name",
                    "area",
                )
            },
        ),
    )

    tabs = [
        ("Property", tab_property),
        ("Facilities", tab_facilities),
    ]
于 2019-04-28T07:34:31.137 回答