<legend id='2Em1P'><style id='2Em1P'><dir id='2Em1P'><q id='2Em1P'></q></dir></style></legend>

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

    <bdo id='2Em1P'></bdo><ul id='2Em1P'></ul>

    <small id='2Em1P'></small><noframes id='2Em1P'>

      1. <tfoot id='2Em1P'></tfoot>

        Django Admin 未保存处于初始状态的预填充内联字段

        时间:2023-11-07
          <tbody id='mrpuG'></tbody>
        • <small id='mrpuG'></small><noframes id='mrpuG'>

          <tfoot id='mrpuG'></tfoot>

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

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

            • <bdo id='mrpuG'></bdo><ul id='mrpuG'></ul>

                  本文介绍了Django Admin 未保存处于初始状态的预填充内联字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些自定义管理代码初始化一些内联子对象.

                  I have some custom admin code which initialises some inline child objects.

                  如果用户编辑内联子对象的默认值之一,则在保存父对象时创建该子元素.

                  If the user edits one of the inline child object's default values, then that child element is created when the parent object is saved.

                  我假设 Django 正在检查值是否已从它们的 initial 值更改,并且仅在用户更改值时才保存.

                  I assume that Django is checking whether values have changed from their initial values and only saving if the user has changed a value.

                  是这样吗?

                  如果用户选择不更改默认值,我如何强制 Django Admin 创建具有未更改默认值的内联子对象?

                  How do I force Django Admin to create inline child objects with their unchanged default values, if the user chooses not to change the default values?

                  class PrepopIpInlineFormSet(forms.models.BaseInlineFormSet):
                      model = Ip
                  
                      def __init__(self, *args, **kwargs):
                          super(PrepopIpInlineFormSet, self).__init__(*args, **kwargs)
                          initial = # calculate a set of default Ip model initial values
                          self.initial = initial
                  
                  
                  class PrepopIpInline(admin.options.InlineModelAdmin):
                      template = "admin/linked.html"
                      model = Ip
                      formset = PrepopIpInlineFormSet
                      fk_name = 'sim'
                      admin_model_path = None
                      show_url = True
                  
                      def __init__(self, *args):
                          super(PrepopIpInline, self).__init__(*args)
                          if self.admin_model_path is None:
                              self.admin_model_path = self.model.__name__.lower()
                  
                      def get_formset(self, request, obj=None, **kwargs):
                          formset = super(PrepopIpInline, self).get_formset(request, obj, **kwargs)
                          formset.request = request
                          return formset
                  
                      def get_extra(self, request, obj=None, **kwargs):
                          if obj:
                              return 0
                          else:
                              return ApnGgsn.objects.all().count()
                  

                  推荐答案

                  来源:如何强制保存空"/未更改的 django admin inline?

                  from django.contrib import admin
                  from django.forms.models import BaseInlineFormSet, ModelForm
                  
                  class AlwaysChangedModelForm(ModelForm):
                      def has_changed(self):
                          """ Should returns True if data differs from initial. 
                          By always returning true even unchanged inlines will get validated and saved."""
                          return True
                  
                  class CheckerInline(admin.StackedInline):
                      """ Base class for checker inlines """
                      extra = 0
                      form = AlwaysChangedModelForm
                  

                  这篇关于Django Admin 未保存处于初始状态的预填充内联字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Django 1.5 中使用多个 AUTH_USER_MODEL 的正确方法是什么? 下一篇:allow_tags=True 不渲染 &lt;form&gt;django admin 中的标签

                  相关文章

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

                2. <small id='qX87j'></small><noframes id='qX87j'>

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

                      <tfoot id='qX87j'></tfoot>