Django - 内联对象的重写方法未调用
class ServiceInline(admin.TabularInline):
model = Service
class PlanAdmin(admin.ModelAdmin):
inlines = [ServiceInline]
class ServiceAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
instance = form.save(commit = False)
if not instance.created_at and not instance.modified_at:
instance.created_by = request.user
instance.modified_by = request.user
instance.save()
form.save_m2m()
return instance
我已经重写了管理类中 Service 类的 save_model(self, request, obj, form,change):
方法。但是,当通过内联(计划页面)添加服务对象时,不会调用此重写的方法。有什么建议吗?
class ServiceInline(admin.TabularInline):
model = Service
class PlanAdmin(admin.ModelAdmin):
inlines = [ServiceInline]
class ServiceAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
instance = form.save(commit = False)
if not instance.created_at and not instance.modified_at:
instance.created_by = request.user
instance.modified_by = request.user
instance.save()
form.save_m2m()
return instance
I have overrided the save_model(self, request, obj, form, change):
method of Service class in it's admin class. But this overrided method is not getting called when a Service object added via Inline (Plan's page). Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与其使用管理中的
save_model
功能(我认为这不起作用),不如使用以下方法:Rather than using the
save_model
function in the admin, which I don’t think is going to work, what about the following:这不是特定于您的用例的答案,但您也许可以使用
post_save
信号解决您的问题:http://docs.djangoproject.com/en/1.2/ref/signals/#post-saveThis is not an answer specific to your use case, but you might be able to work around your problem using
post_save
signal: http://docs.djangoproject.com/en/1.2/ref/signals/#post-save我认为您真正想做的是将 save_model 方法移至 ServiceInline 类中。在本例中,ServiceInline 是服务的管理类。
I think what you really want to do is move that save_model method into the ServiceInline class. The ServiceInline, in this case, is your Admin class for the Service.