<legend id='pfFyW'><style id='pfFyW'><dir id='pfFyW'><q id='pfFyW'></q></dir></style></legend>
    <bdo id='pfFyW'></bdo><ul id='pfFyW'></ul>

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

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

      <tfoot id='pfFyW'></tfoot>

      1. django admin inlines:从 formfield_for_foreignkey 获取对象

        时间:2023-11-07

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

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

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

                • 本文介绍了django admin inlines:从 formfield_for_foreignkey 获取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 django 管理员内联中过滤外键字段中显示的选项.因此,我想访问正在编辑的父对象.我一直在研究,但找不到任何解决方案.

                  I am trying to filter the options shown in a foreignkey field, within a django admin inline. Thus, I want to access the parent object being edited. I have been researching but couldn't find any solution.

                  class ProjectGroupMembershipInline(admin.StackedInline):
                      model = ProjectGroupMembership
                      extra = 1
                      formset = ProjectGroupMembershipInlineFormSet
                      form = ProjectGroupMembershipInlineForm
                  
                      def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
                          if db_field.name == 'group':
                              kwargs['queryset'] = Group.objects.filter(some_filtering_here=object_being_edited)
                          return super(ProjectGroupMembershipInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
                  

                  我在编辑对象时验证了 kwargs 是空的,所以我无法从那里获取对象.

                  I have verified that kwargs is empty when editing an object, so I can't get the object from there.

                  有什么帮助吗?谢谢

                  推荐答案

                  为了过滤可用于管理内联中的外键字段的选项,我重写了表单,以便可以更新表单字段的 queryset属性.这样您就可以访问表单中正在编辑的对象 self.instance.所以是这样的:

                  To filter the choices available for a foreign key field in an admin inline, I override the form so that I can update the form field's queryset attribute. That way you have access to self.instance which is the object being edited in the form. So something like this:

                  class ProjectGroupMembershipInlineForm(forms.ModelForm):
                      def __init__(self, *args, **kwargs):
                          super(ProjectGroupMembershipInlineForm, self).__init__(*args, **kwargs)
                          self.fields['group'].queryset = Group.objects.filter(some_filtering_here=self.instance)
                  

                  如果您执行上述操作,则不需要使用 formfield_for_foreignkey,它应该可以完成您描述的操作.

                  You don't need to use formfield_for_foreignkey if you do the above and it should accomplish what you described.

                  这篇关于django admin inlines:从 formfield_for_foreignkey 获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                      <bdo id='ge8dG'></bdo><ul id='ge8dG'></ul>

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

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

                          <tfoot id='ge8dG'></tfoot>
                        • <small id='ge8dG'></small><noframes id='ge8dG'>