<legend id='aMNiv'><style id='aMNiv'><dir id='aMNiv'><q id='aMNiv'></q></dir></style></legend>

    1. <small id='aMNiv'></small><noframes id='aMNiv'>

      • <bdo id='aMNiv'></bdo><ul id='aMNiv'></ul>
      <i id='aMNiv'><tr id='aMNiv'><dt id='aMNiv'><q id='aMNiv'><span id='aMNiv'><b id='aMNiv'><form id='aMNiv'><ins id='aMNiv'></ins><ul id='aMNiv'></ul><sub id='aMNiv'></sub></form><legend id='aMNiv'></legend><bdo id='aMNiv'><pre id='aMNiv'><center id='aMNiv'></center></pre></bdo></b><th id='aMNiv'></th></span></q></dt></tr></i><div id='aMNiv'><tfoot id='aMNiv'></tfoot><dl id='aMNiv'><fieldset id='aMNiv'></fieldset></dl></div>
    2. <tfoot id='aMNiv'></tfoot>

      1. 从 Django 中的 FilteredSelectMultiple 小部件中获取所选值

        Get the chosen values from FilteredSelectMultiple widget in Django(从 Django 中的 FilteredSelectMultiple 小部件中获取所选值)
        1. <tfoot id='rzi4t'></tfoot>
        2. <i id='rzi4t'><tr id='rzi4t'><dt id='rzi4t'><q id='rzi4t'><span id='rzi4t'><b id='rzi4t'><form id='rzi4t'><ins id='rzi4t'></ins><ul id='rzi4t'></ul><sub id='rzi4t'></sub></form><legend id='rzi4t'></legend><bdo id='rzi4t'><pre id='rzi4t'><center id='rzi4t'></center></pre></bdo></b><th id='rzi4t'></th></span></q></dt></tr></i><div id='rzi4t'><tfoot id='rzi4t'></tfoot><dl id='rzi4t'><fieldset id='rzi4t'></fieldset></dl></div>
            <bdo id='rzi4t'></bdo><ul id='rzi4t'></ul>

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

          • <legend id='rzi4t'><style id='rzi4t'><dir id='rzi4t'><q id='rzi4t'></q></dir></style></legend>
              <tbody id='rzi4t'></tbody>

                  本文介绍了从 Django 中的 FilteredSelectMultiple 小部件中获取所选值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Django admin 中,有谁知道在保存表单时如何从 FilteredSelectMultiple 小部件中获取选择的值?

                  类 ControllerForm(forms.ModelForm):终端 = forms.ModelMultipleChoiceField(queryset=[])def __init__(self, *args, **kwargs):super(ControllerForm, self).__init__(*args, **kwargs)self.fields['terminal'].widget = widgets.FilteredSelectMultiple('terminals', False)self.fields['terminal'].help_text = "选择要添加到组中的终端."self.fields['terminal'].required = Falseself.fields['terminal'].label = "选择终端"self.fields['terminal'].choices = [(t.id, str(t)) for t in Terminal.objects.filter(associated=False)]元类:模型 = 控制器类 ControllerAdmin(admin.ModelAdmin):"""控制器管理表单自定义."""list_display = ('name', 'group',)表格 = 控制器表格admin.site.register(控制器,ControllerAdmin)

                  我想我可以访问 save_model 方法中的值.(https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model)

                  解决方案

                  我已经找到了解决方案.使用 ModelAdmin 中可用的 save_model 方法,可以访问 form.cleaned_data 字典中选择的对象.

                  def save_model(self, request, obj, form, change):对于 form.cleaned_data['terminal'] 中的终端:...obj.save()

                  结帐https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model 和 https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs 了解有关此方法的更多详细信息.p>

                  谢谢

                  In Django admin , does anyone know how can i get the chosen values from FilteredSelectMultiple widget in, when the form is saved?

                  class ControllerForm(forms.ModelForm):
                  terminal = forms.ModelMultipleChoiceField(queryset=[])
                  
                      def __init__(self, *args, **kwargs):
                          super(ControllerForm, self).__init__(*args, **kwargs)
                          self.fields['terminal'].widget = widgets.FilteredSelectMultiple('terminals', False)
                          self.fields['terminal'].help_text = "Select the terminals which are to be added to the group."
                          self.fields['terminal'].required = False
                          self.fields['terminal'].label = "Select terminal(s)"
                          self.fields['terminal'].choices = [(t.id, str(t)) for t in Terminal.objects.filter(associated=False)]
                      class Meta:
                          model = Controller
                  class ControllerAdmin(admin.ModelAdmin):
                      """
                      Controller admin form customization.
                      """
                      list_display = ('name', 'group',)
                  
                      form = ControllerForm
                  
                  admin.site.register(Controller, ControllerAdmin)
                  

                  EDIT: I think i can access the values in the save_model method. (https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model)

                  解决方案

                  I've reached a solution. Using the save_model method available in the ModelAdmin one can access the chosen objects in the form.cleaned_data dictionary.

                  def save_model(self, request, obj, form, change):
                  
                      for terminal in form.cleaned_data['terminal']:
                          ...
                  
                      obj.save()
                  

                  Checkout https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model and https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs for more details on this method.

                  Thanks

                  这篇关于从 Django 中的 FilteredSelectMultiple 小部件中获取所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                  <legend id='dQdB5'><style id='dQdB5'><dir id='dQdB5'><q id='dQdB5'></q></dir></style></legend>

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

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

                        <tbody id='dQdB5'></tbody>

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