如何在Django管理员中显示ManyToMany关系的raw_id值? [英] How to show raw_id value of a ManyToMany relation in the Django admin?

查看:623
本文介绍了如何在Django管理员中显示ManyToMany关系的raw_id值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在ForeignKeyField和ManyToManyField上使用raw_id的应用程序。管理员显示编辑框右侧的外键值。



不幸的是,它不适用于ManyToMany。我检查了代码,我认为这是正常的行为。但是我想知道有人有一个简单的提示来改变这种行为吗?



提前感谢



更新:我试图子类化ManyToManyRawIdWidget,但我不知道如何说raw_id_fields应该使用我的自定义小部件。 formfield_overrides似乎不适用于raw_id字段

解决方案

最后我成功地使其工作。



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $


$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ rel,attrs = None)
super(VerboseManyToManyRawIdWidget,self).__ init __(rel,attrs)

def label_for_value(self,value):
values = value.split ',')
str_values = []
key = self.rel.get_related_field()。name
for v in values:
try:
obj = self。 rel.to._default_manager.using(self.db).get(** {key:v})
str_values + = [escape(u'%s'%obj)]
除了self.rel .to.DoesNotExist:
str_values + = [u'???']
return u'& nbsp;< strong>%s< / strong>'%(u',& ;穰n(str_values))


class MyAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self,db_field,** kwargs):
如果db_field.name在('foo','bar'):
try:
del kwargs ['request']
除了KeyError:
pass
kwargs ['widget'] = VerboseManyToManyRawIdWidget(db_field.rel)
返回db_field.formfield(** kwargs)
返回超级(MyAdmin,self).formfield_for_dbfield(db_field,** kwargs)

不幸的是,我花了一大笔钱没有任何东西; - )



更新: ve为此创建了一个django代码片段: http://djangosnippets.org/snippets/2108/


I have an app using raw_id on both ForeignKeyField and ManyToManyField. The admin displays the value of the foreign key on the right of the edit box.

Unfortunatey, it doesn't work with ManyToMany. I've checked the code and I think that it is the normal behavior. However I would like to know if someone has an easy tip to change this behavior?

Thanks in advance

Update: I've tried to subclass the ManyToManyRawIdWidget but I don't know how to say that the raw_id_fields should use my custom widget. formfield_overrides doesn't seem to work with raw_id fields

解决方案

Finally I succeed to make it working.

from django.contrib.admin.widgets import ManyToManyRawIdWidget

class VerboseManyToManyRawIdWidget(ManyToManyRawIdWidget):
    def __init__(self, rel, attrs=None):
        super(VerboseManyToManyRawIdWidget, self).__init__(rel, attrs)

    def label_for_value(self, value):
        values = value.split(',')
        str_values = []
        key = self.rel.get_related_field().name
        for v in values:
            try:
                obj = self.rel.to._default_manager.using(self.db).get(**{key: v})
                str_values += [escape(u'%s' % obj)]
            except self.rel.to.DoesNotExist:
                str_values += [u'???']
        return u'&nbsp;<strong>%s</strong>' % (u',&nbsp;'.join(str_values))


class MyAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name in ('foo', 'bar'):
            try:
                del kwargs['request']
            except KeyError:
                pass
            kwargs['widget'] = VerboseManyToManyRawIdWidget(db_field.rel)
            return db_field.formfield(**kwargs)
        return super(MyAdmin,self).formfield_for_dbfield(db_field,**kwargs)

Unfortunately, I've spend a bounty for nothing ;-)

UPDATE: I've created a django snippet for this : http://djangosnippets.org/snippets/2108/

这篇关于如何在Django管理员中显示ManyToMany关系的raw_id值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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