访问 django admin 内联模型中的请求对象

发布于 2024-11-30 06:14:25 字数 847 浏览 0 评论 0原文

我有以下模型/表单/管理层次结构。我想要实现的基本上是将当前登录的用户添加到 AttachmentInlines 的字段中。所以我想我需要在几个方法中传递的请求对象,在下面实现。 但是,在这种情况下,这些方法都不会被调用,显然是因为我正在使用内联模型。

我可以在 InvoiceAdmin#save_model 中工作,但这很糟糕,因为我将不得不在一大堆模型管理员中使用相同的内联。对我来说这似乎不太干。

那么有没有其他方法可以从管理中的内联模型中访问请求对象?

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...'

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...'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文