accessing request object within a django admin inl

2019-05-10 08:04发布

I have the following model / form / admin hierarchy. What I want to achieve is basically, add the currently logged in user to a field of the AttachmentInlines. So I thought I would need the request object which gets passed in at several methods, implemented below. However, none of these methods gets called in that context, obviously because I am working with Inline models.

I COULD work in InvoiceAdmin#save_model, but that sucks because I would then have to that in a whole bunch of model admins all using the same Inlines. Which doesn't seem too DRY to me.

So is there any other way to get access to the request object from within my Inline models in the admin?

class AttachmentForm(forms.ModelForm):
    class Meta:
        model = Attachment

    def save(self, request, obj, *args, **kwargs):
        print 'I never get called :-('


class AttachmentInlines(generic.GenericStackedInline):
    model = Attachment
    form = AttachmentForm

    def save_model(self, request, obj, form, change):
        print 'I never get called either...'


class InvoiceAdmin(admin.ModelAdmin):
    inlines = [ AttachmentInlines, ]

    def save_model(self, request, obj, form, change):
        print 'I DO get called, but I am not needed...'

0条回答
登录 后发表回答