Django Inline:如何在change_view()django中获取内联属性

时间:2018-11-12 06:57:29

标签: django django-models django-admin

电子邮件模型类

 class Email(models.Model)
       body_message = models.TextField(max_length = 256, null = False)
       #other fields ForeignKey, subject, to etc.

Tabular内联模型。

#Inline Email
class EmailInline(admin.TabularInline):
model = Email

这是我的RequestAdmin模型,显示内联。

class RequestAdmin(admin.ModelAdmin):
      inlines = [EmailInline,]

      #In change_view() i want to print my inlines properties 

      def change_view(self, request, object_id, form_url='', extra_context=None):

                for inline in self.inlines:
                      #Here i can remove my specific inline
                      self.inlines.remove(inline)
                      #But i want to check my inline_class properties
                      print(inline.body_message) #This line raise exception
                      #i want to print inlines body_message


      return super(RequestAdmin, self).change_view(request, object_id)

我得到的错误图像。

https://medium.com/xcblog/painless-json-parsing-with-swift-codable-2c0beaeb21c1

现在如何获取内联属性(body_message)?

1 个答案:

答案 0 :(得分:1)

我想我对您的问题的理解会更好一些-您想影响正在填充到内联表单中的实际对象,并根据body_message属性的内容隐藏某些对象。在这种情况下,您可以覆盖InlineModelAdmin.get_queryset方法,这将使您有机会排除某些项目;或ModelAdmin.get_inline_formsets方法,这使您有机会更改将为嵌入式对象生成的表单:

from django.contrib.admin.options import TabularInline

class EmailInline(TabularInline):
    model = Email

    # If you want to manipulate the query used to select the related objects:
    def get_queryset(self, request):
        queryset = super(EmailInline, self).get_queryset(request)
        modified_queryset = queryset.exclude(body_message__contains='some string')
        return modified_queryset


class RequestAdmin(admin.ModelAdmin):
    inlines = [EmailInline, ]

    # If you wanted to manipulate the inline forms, to make one of the fields read-only:
    def get_inline_formsets(self, request, formsets, inline_instances, obj=None):
        inline_admin_formsets = []
        for inline, formset in zip(inline_instances, formsets):
            fieldsets = list(inline.get_fieldsets(request, obj))
            readonly = list(inline.get_readonly_fields(request, obj))
            prepopulated = dict(inline.get_prepopulated_fields(request, obj))
            inline_admin_formset = helpers.InlineAdminFormSet(
                inline, formset, fieldsets, prepopulated, readonly,
                model_admin=self,
            )

            # Custom code here - analyze the forms/objects, make changes to the form if desired:
            if isinstance(inline, EmailInline):
                for form in inline_admin_formset.forms:
                    if 'some string' in form.instance.body_message:
                        # this adds the "readonly" attribute to the input widget for the `body_message` field:
                        form.fields['body_message'].widget.attrs['readonly'] = True

            inline_admin_formsets.append(inline_admin_formset)
        return inline_admin_formsets