Django Inline : how to get inline properties in ch

2019-08-21 09:53发布

Email model class

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

Tabular Inline model.

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

And this is my RequestAdmin model that shows inlines.

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)

Error image which i am getting.

Exception Value:
type object 'EmailInline' has no attribute 'body_message'

Now how i can get my inline properties (body_message) ?

1条回答
老娘就宠你
2楼-- · 2019-08-21 10:05

I think I understand your question a bit better - you want to affect the actual objects that are being populated into the inline form(s), and hide certain ones based on the contents of the body_message property. In that case, you can override the InlineModelAdmin.get_queryset method, which will give you the opportunity to exclude certain items; or the ModelAdmin.get_inline_formsets method, which gives you the opportunity to alter the forms that will be generated for the inline objects:

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
查看更多
登录 后发表回答