删除关系时,Django Python m2m_change 不起作用
Posted
技术标签:
【中文标题】删除关系时,Django Python m2m_change 不起作用【英文标题】:Django Python m2m_change does not work when removing the relation 【发布时间】:2013-07-05 09:17:31 【问题描述】:我正在编写一个与课程相关的项目。但是,当我使用 Django admin 添加 CourseSession 时,save() 不起作用。但是当我编辑它然后保存时,它会起作用。请帮我。以下是课程 CourseSession 和 Course。我这里做的是在添加相关课程会话时自动更新每门课程的讲师。(更新:我使用了m2m_change功能,但删除课程会话时不起作用)
def course_session_instructor_changed(sender, instance, action, **kwargs):
superCourse = instance.course
superCourse.instructors.clear()
course_session_set = superCourse.course_session.all()
for each_course_session in course_session_set:
# add instructor
if action=="post_add":
instructors = each_course_session.instructors.all()
for instructor in instructors:
if not instructor in superCourse.instructors.all():
superCourse.instructors.add(instructor)
# remove instructor
elif action=="pre_remove" :
if not each_course_session == instance:
instructors = each_course_session.instructors.all()
for instructor in instructors:
if not instructor in superCourse.instructors.all():
superCourse.instructors.add(instructor)
superCourse.save()
m2m_changed.connect(course_session_instructor_changed, sender=CourseSession.instructors.through)
class CourseSession(models.Model):
course = models.ForeignKey('Course', related_name='course_session')
instructors = models.ManyToManyField(User, related_name = 'instructor_course_session')
enrollment = models.ManyToManyField(User, related_name = 'course_enrollment')
start = models.DateField()
# Weeks of duration
duration = models.IntegerField()
# capacity of the session
max_cap = models.IntegerField()
questionSet = models.ManyToManyField(QuestionSet, blank=True, null=True, related_name='session_questionSet')
class Meta:
verbose_name = _('Session')
verbose_name_plural = _('Sessions')
get_latest_by = "start"
def __unicode__(self):
return unicode(self.instructors.all())+unicode(self.course)+unicode(self.start)
def is_started(self):
return date.today()> self.start
def is_expired(self):
length = timedelta(days = self.duration*7)
return self.start+length< date.today()
def get_enrollment(self):
return self.enrollment.count()
**class Course(models.Model):
name = models.CharField(_('Course Name'),max_length=256)
# Simple Introduction
brief_intro = models.CharField(_('Brief Intro'),max_length=1024)
intro = models.TextField()
learning_obj = models.TextField()
creator = models.ForeignKey(User, related_name = 'course_creator')
created = models.DateTimeField(auto_now_add=True)
cover = models.ImageField(upload_to = 'course/covers/')
institute = models.ForeignKey('Institute', related_name='institute_courses')
workload = models.IntegerField()
assignments = models.IntegerField()
exams = models.IntegerField()
knowledge_tree_root = models.ForeignKey(Topic, related_name='knowledge_tree_root')
instructors = models.ManyToManyField(User, related_name='courses', null=True, blank=True)
#tree_root = models.ForeignKey('Topic')
class Meta:
verbose_name = _('Course')
verbose_name_plural = _('Courses')
def __unicode__(self):
return self.name
【问题讨论】:
【参考方案1】:第一次无法使用,因为在保存一次实例之前无法设置多对多字段(因为它们保存在单独的表中,并且必须有一个 ID 才能链接到)。所以管理员在保存之前不会设置值。
您可能想要使用m2m_changed
signal,而不是覆盖保存。
【讨论】:
我写了一个 m2m_change 函数。它确实在第一次工作。但是,当我删除课程会话时,它不会被触发。你能告诉我为什么吗?以上是关于删除关系时,Django Python m2m_change 不起作用的主要内容,如果未能解决你的问题,请参考以下文章