简体   繁体   English

Django - 在admin中保存更新另一个模型

[英]Django - update another model on save in admin

I am using django to create a video library website. 我正在使用django创建一个视频库网站。

I have 3 models for the library: Video, VideoTopic, and Course. 我有3个模型库:Video,VideoTopic和Course。

I have a TimedeltaField for the duration of each Video. 我在每个视频的持续时间内都有一个TimedeltaField。 Upon saving each video in admin, I want to get the sum of each TimedeltaField for all Videos within a Course, then save that sum to a TimedeltaField in my Course model. 在admin中保存每个视频后,我想获取课程中所有视频的每个TimedeltaField的总和,然后将该总和保存到我的课程模型中的TimedeltaField。

I'm using django-timedeltafield , which will handle the summarization very well. 我正在使用django-timedeltafield ,它将很好地处理摘要。 What I have yet to find a solution for is how to update the Course model after I save each Video in admin. 我还没有找到解决方案是如何在管理员中保存每个视频后更新课程模型。 Any advice? 有什么建议?

My models are as follows: 我的模型如下:

class Video(models.Model):
    title = models.CharField(max_length=200)
    topic = models.ForeignKey('VideoTopic')
    duration = timedelta.fields.TimedeltaField(default="3 minutes, 30 seconds", help_text="(x minutes, x seconds)")

class VideoTopic(models.Model):
    name = models.CharField(max_length=200, blank=True)
    course = models.ForeignKey('course.Course')

class Course(models.Model):
    name = models.CharField(max_length=200)
    duration = timedelta.fields.TimedeltaField()

I recommend you use a django signal . 我建议你使用django信号 This will ensure your models are synchronized regardless of where they are updated (in the app, admin, etc.). 这将确保您的模型同步,无论它们在何处更新(在应用程序,管理员等)。

Register the post save signal on the Video. 在视频上注册保存后信号。 In the signal, you will receive the video instance. 在信号中,您将收到视频实例。 Simply query for its related records in VideoTopic and Course and do the math there. 只需在VideoTopic和Course中查询其相关记录,然后在那里进行数学计算。 I recommend doing all this in the transaction if you don't want it to ever fail. 如果您不想让它失败,我建议您在交易中完成所有这些操作。 If you don't need the information always up-to-date, it's also an opportunity to do some background work to make the save lighter weight. 如果您不需要始终保持最新的信息,那么也可以进行一些后台工作,以节省更轻的重量。

Why a signal? 为何发出信号?

  • Separates concerns 分开关注
  • Ensures your logic always runs 确保您的逻辑始终运行
  • Can help decouple things out of the model directly if it has dependencies, which can cut down on some situations of loading order/circular dependencies. 如果它具有依赖性,可以帮助直接将模型与模型分离出来,这可以减少加载顺序/循环依赖性的某些情况。

Alternatively, you could simply override the Video model's save method and do the same thing there. 或者,您可以简单地覆盖视频模型的保存方法并在那里执行相同的操作。 In either case, just use the related properties in the ORM from your foreign keys if you don't want to write the queries yourself. 在任何一种情况下,如果您不想自己编写查询,只需使用外键中的ORM中的相关属性即可。

An alternative is to override the admin save method to update the course model. 另一种方法是覆盖admin save方法来更新课程模型。 See https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-methods . 请参阅https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-methods

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM