【发布时间】:2013-11-14 23:16:17
【问题描述】:
我想更新manytomany字段,其他人的回答是先删除然后添加,但我需要知道是否可以只更新,可能是模型:
class CtlgTypeExercise(models.Model):
type_exercise = models.CharField(max_length=50)
class CtlgExercise(models.Model):
exercise = models.CharField(max_length=250)
time = models.IntegerField()
ctlg_type_exercise = models.ForeignKey('CtlgTypeExercise')
class UsrExercisePlan(models.Model):
user = models.ManyToManyField(User)
date = models.DateField()
ctlg_exercise = models.ManyToManyField('CtlgExercise')
在 CtlgTypeExercise 中有:跑步、游泳、自行车......
在 CtlgExercise 中有:“在街上跑步 20 分钟”,输入为“跑步”,或“在山上跑步 10 分钟”,输入“跑步”,或“游泳 30 分钟”,输入“游泳”... ..
在 UsrExercisePlan 中保存用户和锻炼,例如 user:"Peter", date:"Today", ctlg_exercise:"Run 20 minutes in the street" 以及 id。现在我想每“游泳 30 分钟”更换或更新 ctlg_exercise。
什么是最好的更新方式,我不想删除和添加,谢谢。
【问题讨论】: