django 管理更改列表中的 ManyToManyField 小部件?

发布于 2024-12-07 16:52:24 字数 1260 浏览 1 评论 0原文

在 django 管理的更改列表中,我想使用 list_editable 来显示 ManyToManyField 的 django-autocomplete 小部件。

我在这里发现了类似的东西: list_editable 和 widgets

通常在 list_display 中包含 ManyToManyField 会引发 ImproperlyConfigured 异常,例如:

""'BookAdmin.list_display[2]','author' 是一个不受支持的 ManyToManyField。"

我(也许不明智)从 contrib/admin/validate.py 中删除了 3 行来绕过异常。:)

我现在有了它吐出以下内容,很接近(?)但没有雪茄。

<位于 0x1032a85d0 的 django.db.models.fields.lated.ManyRelatedManager 对象>

关于如何进行有什么想法吗?有更好的方法吗?

这是我目前所拥有的:(AuthorAutocomplete 在常规管理表单中工作正常)

class AuthorAutocomplete(AutocompleteSettings):
    search_fields = ('first_name', 'last_name')

class BookAdmin(AutocompleteAdmin, admin.ModelAdmin):
    def get_changelist_form(self, request, **kwargs):
      kwargs.setdefault('form', AuthorAutocompleteForm)
      return super(BookAdmin, self).get_changelist_form(request, **kwargs)    
      list_display = ['isbn', 'title', 'author', 'publisher']
      #...

class AuthorAutocompleteForm(forms.ModelForm):
    class Meta:
        model  = Book
    author = AuthorAutocomplete

谢谢!

In the change list of the django admin, I want to use list_editable to display a django-autocomplete widget for a ManyToManyField.

I found something similar here: list_editable and widgets

Normally including a ManyToManyField in list_display raises an ImproperlyConfigured exception, eg:

""'BookAdmin.list_display[2]', 'author' is a ManyToManyField which is not supported."

I (perhaps unwisely) removed 3 lines from contrib/admin/validate.py to bypass the exception. :)

I now have it spitting out the following, which is close(?) but no cigar.

<django.db.models.fields.related.ManyRelatedManager object at 0x1032a85d0>

Any thoughts on how to proceed? Is there a better way of doing this?

Here's what I have at the moment: (AuthorAutocomplete is working fine in the regular admin form)

class AuthorAutocomplete(AutocompleteSettings):
    search_fields = ('first_name', 'last_name')

class BookAdmin(AutocompleteAdmin, admin.ModelAdmin):
    def get_changelist_form(self, request, **kwargs):
      kwargs.setdefault('form', AuthorAutocompleteForm)
      return super(BookAdmin, self).get_changelist_form(request, **kwargs)    
      list_display = ['isbn', 'title', 'author', 'publisher']
      #...

class AuthorAutocompleteForm(forms.ModelForm):
    class Meta:
        model  = Book
    author = AuthorAutocomplete

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔岸观火 2024-12-14 16:52:24

要在您自己的代码中获取 ManyToMany 字段的值以便可以显示它们的值,您可以执行以下操作。我使用 list_display 作为示例。

class TestAdmin(admin.ModelAdmin):
    def get_some_value(self):
        return ", " . join([x.__str__() for x in self.manytomany_field.all()])
    get_some_value.short_description = 'Title'  # sets column header title

    list_display = ('get_some_value',)  # define Model's fields to be presented on admin changelist

其中 manytomany_field您为 manytomany_field 指定的名称,get_some_value 是类中的一个方法,被分配了一个short_description

To get the values for a ManyToMany field in your own code so you can display their values you can do the following. I'm using list_display as an example.

class TestAdmin(admin.ModelAdmin):
    def get_some_value(self):
        return ", " . join([x.__str__() for x in self.manytomany_field.all()])
    get_some_value.short_description = 'Title'  # sets column header title

    list_display = ('get_some_value',)  # define Model's fields to be presented on admin changelist

where manytomany_field is the name you gave your manytomany_field and get_some_value is a method within the class which is assigned a short_description.

绝不服输 2024-12-14 16:52:24

愚蠢的我。我认为 William 的 Manytomany_field 是一个内置的 ModelAdmin 对象。所以我按原样运行他的代码(在加入后添加缺少的括号后)。

奇怪的是,它运行时没有产生错误消息。但是当我应该获取值时(无)出现了。我觉得很奇怪,当我用 Google 搜索“django model.Admin.manytomany_field”时找不到任何东西。哈!

所以最终我意识到我应该用多对多字段的 NAME 来代替 Manytomany_field 。有用!

Silly me. I thought that William's manytomany_field was a built-in ModelAdmin object. So I ran his code as is (after putting in the missing parenthesis after join).

Strange to say, it ran without producing an error message. But (None) appeared when I was supposed to get values. And I thought it was strange that I couldn't find anything when I Googled "django model.Admin.manytomany_field". Hah!

So eventually I realized that I was to put the NAME of the many-to-many field in place of manytomany_field. It works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文