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

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

      <legend id='JcQcZ'><style id='JcQcZ'><dir id='JcQcZ'><q id='JcQcZ'></q></dir></style></legend>
      1. 如何从 Django 中的 inlineadmin 获取当前模型实例

        How to get the current model instance from inlineadmin in Django(如何从 Django 中的 inlineadmin 获取当前模型实例)
        • <bdo id='n6zqe'></bdo><ul id='n6zqe'></ul>

                  <tbody id='n6zqe'></tbody>

              • <small id='n6zqe'></small><noframes id='n6zqe'>

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

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

                  本文介绍了如何从 Django 中的 inlineadmin 获取当前模型实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 formfield_for_manytomany.formfield_for_foreignkey" rel="noreferrer">django 文档.但在该函数中,我需要获取当前正在编辑的父对象.

                  I am using the formfield_for_manytomany given in django documentation. But inside that function I need to get the current parent object being edited.

                  def formfield_for_manytomany(self, db_field, request, **kwargs):
                      if db_field.name == "car":
                          kwargs["queryset"] = Cars.objects.filter(owner=person)
                      return super(myModel, self).formfield_for_manytomany(db_field, request, **kwargs)
                  

                  如何获取正在编辑的人?

                  How can I get the person being edited?

                  推荐答案

                  如果 person 无法从 request 轻松获取,则可能需要通过覆盖手动传递ModelAdmin.get_form()InlineModelAdmin.get_formset():

                  If the person cannot be easily got from request, you may need to manually pass it by overriding ModelAdmin.get_form() or InlineModelAdmin.get_formset():

                  from functools import partial
                  
                  class MyModelAdmin(admin.ModelAdmin):
                      def get_form(self, request, obj=None, **kwargs):
                          kwargs['formfield_callback'] = partial(self.formfield_for_dbfield, request=request, obj=obj)
                          return super(MyModelAdmin, self).get_form(request, obj, **kwargs)
                  
                      def formfield_for_dbfield(self, db_field, **kwargs):
                          person = kwargs.pop('obj', None)
                          formfield = super(MyModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
                          if db_field.name == "car" and person:
                              formfield.queryset = Cars.objects.filter(owner=person)
                          return formfield 
                  
                  # or its inline
                  class MyInlineModelAdmin(admin.StackedInline):
                      def get_formset(self, request, obj=None, **kwargs):
                          kwargs['formfield_callback'] = partial(self.formfield_for_dbfield, request=request, obj=obj)
                          return super(MyInlineModelAdmin, self).get_formset(request, obj, **kwargs)
                  
                      def formfield_for_dbfield(self, db_field, **kwargs):
                          person = kwargs.pop('obj', None)
                          formfield = super(MyInlineModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
                          if db_field.name == "car" and person:
                              formfield.queryset = Cars.objects.filter(owner=person)
                          return formfield 
                  

                  或者

                  class MyModelAdmin(admin.ModelAdmin):
                      def get_form(self, request, obj=None, **kwargs):
                          kwargs['formfield_callback'] = partial(self.formfield_for_dbfield, request=request, obj=obj)
                          return super(MyModelAdmin, self).get_form(request, obj, **kwargs)
                  
                      def formfield_for_dbfield(self, db_field, **kwargs):
                          if db_field.name != "car":
                              kwargs.pop('obj', None)
                          return super(MyModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
                  
                      def formfield_for_manytomany(self, db_field, request=None, **kwargs):
                          person = kwargs.pop('obj', None)
                          if db_field.name == "car" and person:
                              kwargs['queryset'] = Cars.objects.filter(owner=person)
                          return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
                  

                  这篇关于如何从 Django 中的 inlineadmin 获取当前模型实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='pAWOZ'><style id='pAWOZ'><dir id='pAWOZ'><q id='pAWOZ'></q></dir></style></legend>
                      <tbody id='pAWOZ'></tbody>

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

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

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