访问 django admin 内联模型中的请求对象
我有以下模型/表单/管理层次结构。我想要实现的基本上是将当前登录的用户添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论