Django models: Use multiple values as a key?
- by Rosarch
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()