Django models: Use multiple values as a key?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-05-03T16:20:48Z Indexed on 2010/05/03 16:38 UTC
Read the original article Hit count: 359

Filed under:
|
|
|

Here is a simple model:

class TakingCourse(models.Model):
    course = models.ForeignKey(Course)
    term = models.ForeignKey(Term)

Instead of Django creating a default primary key, I would like to use both course and term as the primary key - taken together, they uniquely identify a tuple. Is this allowed by Django?

On a related note: I am trying to represent users taking courses in certain terms. Is there a better way to do this?

class Course(models.Model):
    name = models.CharField(max_length=200)
    requiredFor = models.ManyToManyField(RequirementSubSet, blank=True)
    offeringSchool = models.ForeignKey(School)

    def __unicode__(self):
        return "%s at %s" % (self.name, self.offeringSchool)

class MyUser(models.Model):
    user = models.ForeignKey(User, unique=True)
    takingReqSets = models.ManyToManyField(RequirementSet, blank=True)
    takingTerms = models.ManyToManyField(Term, blank=True)
    takingCourses = models.ManyToManyField(TakingCourse, blank=True)
    school = models.ForeignKey(School)

class TakingCourse(models.Model):
    course = models.ForeignKey(Course)
    term = models.ForeignKey(Term)

class Term(models.Model):
    school = models.ForeignKey(School)
    isPrimaryTerm = models.BooleanField()

© Stack Overflow or respective owner

Related posts about django

Related posts about models