<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 的反面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
                      <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'>