Django - 内联对象的重写方法未调用

发布于 2024-10-03 02:06:22 字数 660 浏览 3 评论 0原文

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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-10-10 02:06:22

与其使用管理中的 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)

Rather than using the save_model function in the admin, which I don’t think is going to work, what about the following:

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)
戏剧牡丹亭 2024-10-10 02:06:22

这不是特定于您的用例的答案,但您也许可以使用 post_save 信号解决您的问题:http://docs.djangoproject.com/en/1.2/ref/signals/#post-save

This 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

℉絮湮 2024-10-10 02:06:22

我认为您真正想做的是将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文