I'm trying to create a system that automatically generates lessons based upon a rule I've created using database fields. The rule would hold a list of students and teachers as many-to-many relationships.
I want to directly transfer the relations from the course instance to the lesson instance through a post_save signal.
These are my models
class Course(models.Model):
students = models.ManyToManyField(
Profile,
limit_choices_to=Q(is_teacher=False),
related_name='course_students')
teachers = models.ManyToManyField(
Profile,
limit_choices_to=Q(is_teacher=True),
related_name='course_teachers')
class Lesson(models.Model):
students = models.ManyToManyField(
Profile,
limit_choices_to=Q(is_teacher=False),
related_name='lesson_students')
teachers = models.ManyToManyField(
Profile,
limit_choices_to=Q(is_teacher=True),
related_name='lesson_teachers')
This is my signal code
@receiver(post_save, sender=Course)
def new_course_lesson_creator(sender, instance, created, **kwargs):
if not created:
return
print "Course Created!"
teachers = instance.teachers.all()
students = instance.students.all()
lesson = Lesson()
lesson.teachers.add(*teachers)
lesson.students.add(*students)
lesson.save()
For whatever reason this doesn't transfer the related student and teacher relations to the lesson. The "teachers" and "students" show up as empty.
Any and all help is appreciated :)
Aucun commentaire:
Enregistrer un commentaire