如何在管理员中保存父项时保存相关的内联django模型?

时间:2022-10-04 14:20:16

In my models, I have an Event class, a Volunteer class, and a Session class. The Session class has a foreign key field for an Event and a Volunteer, and is a unique coupling of both, as well as a date and time. Taken together, Volunteer and Event I think technically have a ManyToMany relationship.

在我的模型中,我有一个Event类,一个Volunteer类和一个Session类。 Session类具有Event和Volunteer的外键字段,并且是两者的唯一耦合,以及日期和时间。综合起来,志愿者和活动我认为在技术上有一个ManyToMany关系。

Using the pre-packaged Django admin, I edit Volunteers and Events with their own admin.ModelAdmin classes respectively. Sessions are edited inline in the Events ModelAdmin.

使用预先打包的Django管理员,我分别使用自己的admin.ModelAdmin类编辑志愿者和事件。会话在Events ModelAdmin中内联编辑。

When I add a new Session to an event in the admin interface, with a Volunteer, I need the Volunteer's hours field to be automatically updated, to reflect however many hours the newly added session lasted (plus all past sessions). Currently, I just have a calculate_hours function in the Volunteer model, which iterates over all sessions each time it is called and finds the sum of the hours. I tried to call it with a custom save function in Session, but it appears never to be called after the Event save function. I would try it in Event, but I have no way to isolate which Volunteers need their hours recalculated. The hours field IS updated if I manually go over to the Volunteer admin page, edit, and then save the Volunteer, but this is pretty unacceptable.

当我在管理界面中向一个活动添加一个新会话时,我需要志愿者小时字段自动更新,以反映新添加的会话持续多个小时(以及所有过去的会话)。目前,我在Volunteer模型中只有一个calculate_hours函数,它在每次调用时迭代所有会话并找到小时的总和。我尝试在Session中使用自定义保存功能调用它,但它似乎永远不会在事件保存功能之后调用。我会在赛事中尝试,但我无法分析哪些志愿者需要重新计算他​​们的时间。如果我手动转到志愿者管理页面,编辑,然后保存志愿者,则会更新小时字段,但这是非常不可接受的。

I see that there are many questions on SO about Django problems when saving inline objects on the admin site, particularly with ManyToMany fields. I'm not sure, after reading many of these questions, if what they say applies in my case--maybe I need to receive a signal somewhere, or include a custom save in a special place, or call save_model in my admin.ModelAdmin class... I just don't know. What is the best way to go about this?

我发现在管理站点上保存内联对象时,有很多关于Django问题的问题,特别是在ManyToMany字段中。在阅读了很多这些问题后,我不确定,如果他们说的话适用于我的情况 - 也许我需要在某个地方收到信号,或者在特殊地方包含自定义保存,或者在我的admin.ModelAdmin中调用save_model上课......我只是不知道。最好的方法是什么?

Code can be found here: Models.py, Admin.py

代码可以在这里找到:Models.py,Admin.py

1 个解决方案

#1


1  

First of all, the relationship you're describing is what called a ManyToMany "through" (you can read about it in the documentation here).

首先,您所描述的关系是称为“通过”的ManyToMany(您可以在此处的文档中阅读它)。

Secondly, I don't understand why you need the 'hours' to be a field at all. Isn't a function enough for this? why save it in the database in the first place? you can just call it every time you need it.

其次,我不明白为什么你需要'小时'才能成为一个领域。这个功能不够用吗?为什么首先将它保存在数据库中?你可以在每次需要时调用它。

Finally, it seems to me you're doing a lot of extra work that I don't understand - why do you need the volunteer time boolean field? If you link a volunteer with an event isn't that enough to know that he was there? And what's the purpose of "counts_towards_volunteer_time"? I'm probably missing some of the logic here, but a lot of that seems wasteful.

最后,在我看来,你做了很多我不理解的额外工作 - 为什么你需要志愿者时间布尔字段?如果你将一名志愿者与一个活动联系起来,那还不足以让他知道他在那里吗?那些“counts_towards_volunteer_time”的目的是什么?我可能在这里错过了一些逻辑,但其中很多似乎都很浪费。

#1


1  

First of all, the relationship you're describing is what called a ManyToMany "through" (you can read about it in the documentation here).

首先,您所描述的关系是称为“通过”的ManyToMany(您可以在此处的文档中阅读它)。

Secondly, I don't understand why you need the 'hours' to be a field at all. Isn't a function enough for this? why save it in the database in the first place? you can just call it every time you need it.

其次,我不明白为什么你需要'小时'才能成为一个领域。这个功能不够用吗?为什么首先将它保存在数据库中?你可以在每次需要时调用它。

Finally, it seems to me you're doing a lot of extra work that I don't understand - why do you need the volunteer time boolean field? If you link a volunteer with an event isn't that enough to know that he was there? And what's the purpose of "counts_towards_volunteer_time"? I'm probably missing some of the logic here, but a lot of that seems wasteful.

最后,在我看来,你做了很多我不理解的额外工作 - 为什么你需要志愿者时间布尔字段?如果你将一名志愿者与一个活动联系起来,那还不足以让他知道他在那里吗?那些“counts_towards_volunteer_time”的目的是什么?我可能在这里错过了一些逻辑,但其中很多似乎都很浪费。