将一个ManyToManyWidget添加到Django Admin中的ManyToManyField的相反方向 [英] Adding a ManyToManyWidget to the reverse of a ManyToManyField in the Django Admin

查看:352
本文介绍了将一个ManyToManyWidget添加到Django Admin中的ManyToManyField的相反方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在Django 1.4中有一个简单的博客应用程序:

  class Post(models.Model):
title = ...
published_on = ...
tags = models.ManyToManyField('Tag')

class标签(models.Model):
name = ...

ie一个帖子有很多标签。在Django管理员中,如果我在<$ c中包含标签,我会得到一个很好的小< select multi> PostAdmin 中的$ c>字段。有没有一个简单的方法来在 TagAdmin < select multi> ) >?我尝试在 TagAdmin 中添加 fields = ['name','posts'] ,并获得未正确配置错误。 (相同的结果为 post_set )。



我没有Django,所以可以鞭打一个适当的AdminForm和管理对象,但我希望有一个Right Way™来做到这一点。

解决方案

这可以做一个自定义从django导入表单

 从django导入表单
从模型导入

import post,Tag

class PostAdminForm(forms.ModelForm):
tags = forms.ModelMultipleChoiceField(
Tag.objects.all(),
widget = admin。 widgets.FilteredSelectMultiple('Tags',False),
required = False,


def __init __(self,* args,** kwargs):
super (PostAdminForm,self).__ init __(* args,** kwargs)
如果self.instance.pk:
self.initial ['tags'] = self.instance.tags.values_list('pk' ,flat = True)

def save(self,* args,** kwargs):
instance = super(PostAdminForm, self).save(* args,** kwargs)
如果instance.pk:
instance.tags.clear()
instance.tags.add(* self.cleaned_data ['tags' ])
return instance

class PostAdmin(admin.ModelAdmin):
form = PostAdminForm

admin.site.register(Post,PostAdmin)

False 可以替换为 True 如果您想要垂直堆叠的小部件。


Let's say I have a simple blog app in Django 1.4:

class Post(models.Model):
    title = …
    published_on = …
    tags = models.ManyToManyField('Tag')

class Tag(models.Model):
    name = …

i.e. a post has many tags. On the Django admin, I get a nice little <select multi> if I include tags in the fields for the PostAdmin. Is there an easy way to include the list of the posts (as a simple <select multi>) in the TagAdmin? I tried putting fields = ['name', 'posts'] in the TagAdmin and got an ImproperlyConfigured error. (same result for post_set).

I'm alright with Django, so could whip up a proper AdminForm and Admin object, but I'm hoping there a Right Way™ to do it.

解决方案

This is possible to do with a custom form.

from django.contrib import admin
from django import forms

from models import Post, Tag

class PostAdminForm(forms.ModelForm):
    tags = forms.ModelMultipleChoiceField(
        Tag.objects.all(),
        widget=admin.widgets.FilteredSelectMultiple('Tags', False),
        required=False,
    )

    def __init__(self, *args, **kwargs):
        super(PostAdminForm, self).__init__(*args, **kwargs)
        if self.instance.pk:
            self.initial['tags'] = self.instance.tags.values_list('pk', flat=True)

    def save(self, *args, **kwargs):
        instance = super(PostAdminForm, self).save(*args, **kwargs)
        if instance.pk:
            instance.tags.clear()
            instance.tags.add(*self.cleaned_data['tags'])
        return instance

class PostAdmin(admin.ModelAdmin):
    form = PostAdminForm

admin.site.register(Post, PostAdmin)

That False in there can be replaced with a True if you want vertically stacked widget.

这篇关于将一个ManyToManyWidget添加到Django Admin中的ManyToManyField的相反方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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