django 管理更改列表中的 ManyToManyField 小部件?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在您自己的代码中获取 ManyToMany 字段的值以便可以显示它们的值,您可以执行以下操作。我使用
list_display
作为示例。其中
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.where
manytomany_field
is the name you gave yourmanytomany_field
andget_some_value
is a method within the class which is assigned ashort_description
.愚蠢的我。我认为 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!