Need help creating model.

22 views
Skip to first unread message

Zach

unread,
Mar 27, 2012, 9:18:12 PM3/27/12
to Django users
I have the following in my Student model. I am wanting to track the
date of each point given to each student. The idea would be so that I
could see not only how many points each student has, but also see the
date each point was given. In the future I want to see the trend of
each students' points. How should I go about this? Should I use a
Foreign Key in another class. I am new to this so thanks for reading.


class Student(models.Model):

CLASS_CHOICES = (
(u'Yoga','Yoga'),
(u'Spanish', 'Spanish'),
(u'French', 'French'),
(u'Dance', 'Dance'),
)

name = models.CharField(max_length=30)
points = models.IntegerField(max_length=4)
classname = models.CharField("Class Name",max_length=20, choices =
CLASS_CHOICES)

Psamathos

unread,
Mar 28, 2012, 12:11:39 AM3/28/12
to django...@googlegroups.com
I would create a many-to-many relationship between Student and Class through a custom intermediate model called Grade. You can then add a date field to Grade and any other extra fields you want.

Read up on m2m relationships here: https://docs.djangoproject.com/en/dev/topics/db/models/#many-to-many-relationships

Basically, you want to add a field to Student like
    classes = models.ManyToManyField(Class, through='Grade')

Then add a new model like this:

class Grade(models.Model):
student = models.ForeignKey(Student)
class = models.ForeignKey(Class)
grade = models.IntegerField()
date = models.DateField(
auto_now_add=True)

-Psamathos

jondbaker

unread,
Mar 28, 2012, 2:11:59 AM3/28/12
to django...@googlegroups.com
I think the Foreign Key route is the way to go. After setting up the fields on both the Student and Point models and establishing the FK relationship between the two, you could write a table-level method that returned the number of corresponding Points.
Reply all
Reply to author
Forward
0 new messages