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):
方法。但是,当通过Inline(Plan的页面)添加Service对象时,不会调用此覆盖方法。有什么建议?
答案 0 :(得分:0)
而不是在管理员中使用save_model
功能,我认为这不会起作用,如下所示:
class PlanAdmin(admin.ModelAdmin):
# ...
def save_model(self, request, obj, form, change):
instance = form.save(commit=False)
instance.last_updated_by = request.user
instance.save()
class Service(models.Model):
# ...
created_by = models.ForeignKey(User, blank=True)
def save(self, *args, **kwargs):
self.created_by = self.plan.last_updated_by
super(Service, self).save(*args, **kwargs)
答案 1 :(得分:0)
这不是特定于您的用例的答案,但您可以使用post_save
信号解决您的问题:http://docs.djangoproject.com/en/1.2/ref/signals/#post-save
答案 2 :(得分:0)
我认为你真正想做的是将save_model方法移到ServiceInline类中。在这种情况下,ServiceInline是服务的Admin类。