2

I've following scenario:

class CourseTemplate(models.Model):
    title = models.CharField(max_length=70)
    teacher = models.ForeignKey(User)
    description = models.TextField()

    max_students = models.IntegerField()
    sessions = models.ManyToManyField(CourseSession) # e.g. Session 1 Introduction, Session 2 Basics, etc.
    rating = models.ManyToManyFields(StudentRating)
    date_added = models.DateTimeField()

class CourseEnrollment(models.Model):
    course = models.OneToOneField(CourseTemplate) # Each enrollment needs a new CourseTemplate Instance, so I can track it
    students = models.ManyToManyField(User)

Class CourseSession(models.Model):
    title = models.CharField(max_length=50)
    date = models.DateTimeField()
    details = models.CharField(max_length=100)
    address = models.TextField()
    #parent_course = models.ForeignKey(CourseTemplate)

class StudentRating(models.Model):
    student = models.ForeignKey(User)
    rating = models.IntegerField()
    #course = models.ForeignKey(CourseTemplate)

Now a teacher (=User) can create a CourseTemplate with all the required details first. After it's saved, he can create a concrete "enrollment" for e.g. this semester with 5 sessions. Maybe he changes after 8 enrollments some details (e.g. CourseTemplate.description or the course now only has 7 sessions instead of 8).

I'd like to have a 1:1 relationship between each CourseTemplate instance and each CourseEnrollment, so I can see for example: - Teacher X had 2012 three CourseEnrollments, two of them were the same or - which rating has he received for his second course.

The presented "Template" should always be the "newest", so I'd just need to get the latest instance by CourseTemplate.date_added.

Does anyone know how I can avoid this problem?

Thanks a lot!

5
  • Shouldn't that CourseTemplate() in your CourseEnrollment be a one-to-one and not simply a class-level instance? Commented Aug 29, 2012 at 16:53
  • Was that the answer to your whole question? What part of this is specifically about copying? Commented Aug 29, 2012 at 16:55
  • I don't think so. The idea is that a teacher creates a Course(Template) once and then links it to new CourseEnrollments. If there is a change after the third time, I'd need a 1:1 CourseTemplate-CourseEnrollment relation so I have a snapshot of the ratings, description etc. Commented Aug 29, 2012 at 16:58
  • So you just want to know how to clone a CourseTemplate instance to a new one? Commented Aug 29, 2012 at 16:59
  • I think I need to implement a function in CourseEnrollment, which creates a unique copy of CourseTemplate and saves it. a) This might be wrong and b) I don't know how to copy a object instance :) Commented Aug 29, 2012 at 17:02

1 Answer 1

21

You can duplicate any existing django model instance by clearing its primary key, and then saving it again.

ct = CourseTemplate.objects.all()[0]
print ct.pk
# some original pk

ct.pk = None
ct.save()
print ct.pk
# will be a new auto-incremented 
Sign up to request clarification or add additional context in comments.

4 Comments

Will the original 'ct' be still available (especially as reference for other CourseEnrollment instances) after clearing the PK?
Yep. Doesn't hurt the original. The save method just sees that there is no pk and does an insert. You will need to query down the original ct again though, since this ct will now be the new clone.
If you already have the source instance and you'd like to avoid the extra db access you could also use: ct2=copy(ct) where copy came from: from copy import copy. Then clear the pk.
In case you are looking for it too: link to the documentation (django 1.6). Watch out for the special case mentioned there about model inheritance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.