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

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

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

      1. <tfoot id='c6Z6y'></tfoot>
          <bdo id='c6Z6y'></bdo><ul id='c6Z6y'></ul>

        为现有字段设置只读,但允许在 django admin 中创建新的内联时添加

        时间:2023-11-07

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

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

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

              <tfoot id='WzwBz'></tfoot>
                <tbody id='WzwBz'></tbody>
                <bdo id='WzwBz'></bdo><ul id='WzwBz'></ul>

                • 本文介绍了为现有字段设置只读,但允许在 django admin 中创建新的内联时添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个模型,其中一个与另一个内联.我已将内联模型的字段设为只读.

                  I have two models with one of them as inline to other. I have made fields of inline model read only.

                  class FollowUpInLine(admin.TabularInline):
                  model = md.FollowUp
                  extra = 0
                  can_delete = False
                  
                  def get_readonly_fields(self, request, obj=None):
                      if request.user.is_superuser == False:          
                          if obj: # editing an existing object
                                  return self.readonly_fields + (
                                  'follow_up_date',
                                  'status_inquiry',
                                  'remarks',
                                  'followup_done_by',
                                  )
                      return self.readonly_fields
                  

                  但是,当在内联中单击添加另一个"时,这不允许添加新字段,而是将它们更改为带有值无"的标签.如何使字段内联但在添加下一个内联时添加?

                  However this does not allows to add new fields when "Add another" is clicked in inline rather it changes them to label with value "None". How can I make fields inline but add when next inline is to added?

                  推荐答案

                  我找到了答案.我们需要插入一个构造表单,然后像我所做的那样从内联类中调用这个表单,如下所示:

                  I found the answer to this. We need to insert a construct form and then call this form from the inline class as I have done which is shown below:

                  class RequiredInlineFormSet(BaseInlineFormSet):
                  """
                  Generates an inline formset that is required
                  """
                      def _construct_form(self, i, **kwargs):
                      """
                      Override the method to change the form attribute empty_permitted
                      """
                          form = super(RequiredInlineFormSet, self)._construct_form(i, **kwargs)
                          form.empty_permitted = False
                          return form
                  
                  class FollowUpAddInLine(admin.TabularInline):
                      model = md.FollowUp
                      extra = 1
                      formfield_overrides = {
                      models.CharField: {'widget': TextInput(attrs={'size':'20'})},
                      models.TextField: {'widget': Textarea(attrs={'rows':4, 'cols':40})},
                  }
                  
                      can_delete = False
                      formset = RequiredInlineFormSet
                  
                      def has_change_permission(self, request, obj=None):
                          return False
                  
                  class FollowUpListInLine(admin.TabularInline):
                      model = md.FollowUp
                      readonly_fields = ('status', 'follow_up_date', 'followup_status', 'followup_reason', 'remarks', 'followup_done_by')
                      extra = 0
                      can_delete = False
                      formset = RequiredInlineFormSet
                  
                      def has_add_permission(self, request):
                          return False
                  
                  class FollowUpAdminInLine(admin.TabularInline):
                      model = md.FollowUp
                      extra = 1
                      formfield_overrides = {
                      models.CharField: {'widget': TextInput(attrs={'size':'20'})},
                      models.TextField: {'widget': Textarea(attrs={'rows':4, 'cols':40})},
                  }
                  
                      formset = RequiredInlineFormSet
                  
                      def queryset(self, request):
                          return md.FollowUp.objects.filter(owner=request.user)
                  
                  class PackageAdmin(admin.ModelAdmin):
                  """Makes the FollowUp to be added along with the Package"""
                      inlines =(FollowUpListInLine, FollowUpAddInLine)
                      fields = ('date_of_inquiry', 'agent_name', 'type_of_booking',
                                    'no_of_pax', 'source_of_inquiry', 'business_vendor',
                                'travel_date', 'reply_date', 'client_name',
                                'client_email', 'client_contacts', 'inquiry_assigned_to',
                                'inquiry_assigned_by')
                      list_display = ('agent_name', 'date_of_inquiry','status_color')
                      list_filter = ('date_of_inquiry',)
                      can_delete = False
                      list_per_page = 25
                  
                      def get_readonly_fields(self, request, obj=None):
                      if request.user.is_superuser == False:
                          if obj: # editing an existing object
                              return self.readonly_fields + (
                                          'agent_name',
                                          'date_of_inquiry',
                                           )
                  
                          else:
                              self.inlines = (FollowUpAdminInLine,)   
                          return self.readonly_fields
                  
                      def queryset(self, request):
                  """Limit Pages to those that belong to the request's user."""
                          qs = super(PackageAdmin, self).queryset(request)
                          if request.user.is_superuser:
                             return qs
                          return qs.filter(inquiry_assigned_to=request.user)
                  
                  admin.site.register(md.Package,PackageAdmin)
                  

                  这篇关于为现有字段设置只读,但允许在 django admin 中创建新的内联时添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django 管理员.显示分层下拉过滤器 下一篇:在 Admin 中提高 Django ForeignKey 字段的性能

                  相关文章

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

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