jango管理员更改列表中的ManyToManyField小部件

jango管理员更改列表中的ManyToManyField小部件

本文介绍了在Django管理员更改列表中的ManyToManyField小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在这里找到类似的内容:一个href =https://stackoverflow.com/questions/7065982/list-editable-and-widgets> list_editable和小部件

I found something similar here: list_editable and widgets

通常包括一个list_display中的ManyToManyField引发了一个不正确的配置异常,例如:

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

我(可能不明智地)从contrib / admin / validate.py中删除了3行以绕过该异常。 )

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.

任何想法有没有更好的方法?

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

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

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

谢谢!

推荐答案

这是一个旧主题,但我希望这可能有助于别人。

This is an old topic, but I'm hoping this might help someone else.

要获取ManyToMany字段的值,您可以显示其值,您可以执行以下操作。我使用list_display作为一个例子。

To get the values for a ManyToMany field so you can display their values you can do the follow. 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()])

    list_display(get_some_value)
    get_some_value.short_description = 'Title' #sets column header to Title

这篇关于在Django管理员更改列表中的ManyToManyField小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:14