在Django管理变革列表部件ManyToManyField? [英] ManyToManyField widget in a django admin change list?

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

问题描述

在Django管理的变化列表,我想用list_editable用来显示ManyToManyField一个Django的自动完成构件。

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

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

I found something similar here: list_editable and widgets

通常包括一个list_display ManyToManyField引发ImproperlyConfigured例外,例如:

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

,'BookAdmin.list_display [2],作者是不受支持的ManyToManyField。

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

我(也许是不明智)移除的contrib /管理/ 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.

<在0x1032a85d0&GT django.db.models.fields.related.ManyRelatedManager对象;

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

这是如何进行的任何想法?是否有这样做的更好的办法?

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.

要得到一个多对多字段中的值,因此您可以显示它们的值,你可以做后续。我使用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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆