<small id='iI3d7'></small><noframes id='iI3d7'>

          <bdo id='iI3d7'></bdo><ul id='iI3d7'></ul>
        <i id='iI3d7'><tr id='iI3d7'><dt id='iI3d7'><q id='iI3d7'><span id='iI3d7'><b id='iI3d7'><form id='iI3d7'><ins id='iI3d7'></ins><ul id='iI3d7'></ul><sub id='iI3d7'></sub></form><legend id='iI3d7'></legend><bdo id='iI3d7'><pre id='iI3d7'><center id='iI3d7'></center></pre></bdo></b><th id='iI3d7'></th></span></q></dt></tr></i><div id='iI3d7'><tfoot id='iI3d7'></tfoot><dl id='iI3d7'><fieldset id='iI3d7'></fieldset></dl></div>
      1. <legend id='iI3d7'><style id='iI3d7'><dir id='iI3d7'><q id='iI3d7'></q></dir></style></legend>

        <tfoot id='iI3d7'></tfoot>

        在 Django Admin 中将 ManyToManyWidget 添加到 ManyToManyField 的反面

        Adding a ManyToManyWidget to the reverse of a ManyToManyField in the Django Admin(在 Django Admin 中将 ManyToManyWidget 添加到 ManyToManyField 的反面)

          <tbody id='mcCzG'></tbody>
        <legend id='mcCzG'><style id='mcCzG'><dir id='mcCzG'><q id='mcCzG'></q></dir></style></legend>
        <i id='mcCzG'><tr id='mcCzG'><dt id='mcCzG'><q id='mcCzG'><span id='mcCzG'><b id='mcCzG'><form id='mcCzG'><ins id='mcCzG'></ins><ul id='mcCzG'></ul><sub id='mcCzG'></sub></form><legend id='mcCzG'></legend><bdo id='mcCzG'><pre id='mcCzG'><center id='mcCzG'></center></pre></bdo></b><th id='mcCzG'></th></span></q></dt></tr></i><div id='mcCzG'><tfoot id='mcCzG'></tfoot><dl id='mcCzG'><fieldset id='mcCzG'></fieldset></dl></div>

        <small id='mcCzG'></small><noframes id='mcCzG'>

            • <tfoot id='mcCzG'></tfoot>
                <bdo id='mcCzG'></bdo><ul id='mcCzG'></ul>

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

                  问题描述

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

                  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 = …
                  

                  即一个帖子有很多标签.在 Django 管理员上,如果我在 fields 中为 PostAdmin<包含 tags,我会得到一个不错的小 <select multi>/代码>.有没有一种简单的方法可以在 TagAdmin 中包含帖子列表(作为简单的 <select multi>)?我尝试将 fields = ['name', 'posts'] 放入 TagAdmin 并得到 ImproperlyConfigured 错误.(post_set 的结果相同).

                  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).

                  我对 Django 没问题,所以可以创建一个适当的 AdminForm 和 Admin 对象,但我希望有一个 Right Way 来做.

                  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)
                  

                  如果您想要垂直堆叠的小部件,可以将其中的 False 替换为 True.

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

                  这篇关于在 Django Admin 中将 ManyToManyWidget 添加到 ManyToManyField 的反面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                      <legend id='4JW5w'><style id='4JW5w'><dir id='4JW5w'><q id='4JW5w'></q></dir></style></legend>

                      <tfoot id='4JW5w'></tfoot>

                          <bdo id='4JW5w'></bdo><ul id='4JW5w'></ul>

                            <i id='4JW5w'><tr id='4JW5w'><dt id='4JW5w'><q id='4JW5w'><span id='4JW5w'><b id='4JW5w'><form id='4JW5w'><ins id='4JW5w'></ins><ul id='4JW5w'></ul><sub id='4JW5w'></sub></form><legend id='4JW5w'></legend><bdo id='4JW5w'><pre id='4JW5w'><center id='4JW5w'></center></pre></bdo></b><th id='4JW5w'></th></span></q></dt></tr></i><div id='4JW5w'><tfoot id='4JW5w'></tfoot><dl id='4JW5w'><fieldset id='4JW5w'></fieldset></dl></div>
                              <tbody id='4JW5w'></tbody>

                          1. <small id='4JW5w'></small><noframes id='4JW5w'>