<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 小部件中获取所选值

        时间:2023-09-03
        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 小部件中获取所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:有没有办法在 Python 中创建无边框透明窗口? 下一篇:暂停 Jupyter Notebook 小部件,等待用户输入

                  相关文章

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

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