manytomany 水平过滤器不起作用 django admin

manytomany filter horizontal didn't work django admin

LabelLink 模型中添加字段时出现以下错误 目前我正在开发 django-cms 并想更改当前的管理小部件。

完整性错误 /admin/myapp/labellinkmodel/add/ NOT NULL 约束失败:myapp_labellink_links.sort_value

models.py

class LinkPluginModel(CMSPlugin):
    .....
    label = models.CharField(max_length=200)
    link = models.URLField()


class LabelLink(CMSPlugin):
    ...
    links = models.ManyToManyField(LinkPluginModel,related_name="links" ,blank=True)

admin.py

class LabelLinkAdmin(admin.ModelAdmin):
    filter_horizontal = ('links',)

admin.site.register(LabelLink,LabelLinkAdmin)

嗨,索米尔,

In this code, blank = true will not work. Please remove this and check.

创建一个 forms.py 并添加此代码。绝对能解决你的问题。

class LabelLinkForm(ModelForm):

  class Meta:
    model = Model Name
    fields="__all__"

  def __init__(self, *args, **kwargs):
    super(LabelLinkForm, self).__init__(*args, **kwargs)
    self.fields['links'].required = False