Creating foreign key in Django model
I have the following model of my student. I want to keep track of the date of every moment given to each student. The idea would be so that I could see not only how many points each student has, but also the date each point is given. In the future, I want to see the trend of students. How should I do it? Should I be using foreign key in another class. I'm 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)
source to share
I think you can separate both classes (I call it "Course" so as not to contradict the idea of a Python class) and tracking scores (ie scores).
class Student(models.Model):
name = models.CharField(max_length=30)
courses = models.ManyToManyField('Course')
class Course(models.Model):
# Yoga, Spanish, French, etc.
name = models.CharField(max_length=30)
class Score(models.Model):
date = models.DateTimeField(auto_now_add=True)
points = models.IntegerField(max_length=4)
course = models.ForeignKey('Course')
student = models.ForeignKey('Student')
The student can then take many courses and receive a series of points (i.e. test scores?) For each course.
source to share
You might have something like this:
class Student(models.Model):
CLASS_CHOICES = (
(u'Yoga','Yoga'),
(u'Spanish', 'Spanish'),
(u'French', 'French'),
(u'Dance', 'Dance'),
)
name = models.CharField(max_length=30)
classname = models.CharField("Class Name",max_length=20, choices = CLASS_CHOICES)
@property
def points(self):
return self.point_set.count()
class Point(models.Model):
creation_datetime = models.DateTimeField(auto_now_add=True)
student = models.ForeignKey('Student')
Very easy to use:
In [3]: james = Student(classname='Yoga', name='James')
In [4]: james.save()
DEBUG (0.002) INSERT INTO "testapp_student" ("name", "classname") VALUES (James, Yoga); args=['James', 'Yoga']
In [5]: james.points
DEBUG (0.000) SELECT COUNT(*) FROM "testapp_point" WHERE "testapp_point"."student_id" = 1 ; args=(1,)
Out[5]: 0
In [6]: james.point_set.create()
DEBUG (0.001) INSERT INTO "testapp_point" ("creation_datetime", "student_id") VALUES (2012-03-28 09:38:35.593110, 1); args=[u'2012-03-28 09:38:35.593110', 1]
Out[6]: <Point: Point object>
In [7]: james.points
DEBUG (0.001) SELECT COUNT(*) FROM "testapp_point" WHERE "testapp_point"."student_id" = 1 ; args=(1,)
Out[7]: 1
In [8]: james.point_set.create()
DEBUG (0.001) INSERT INTO "testapp_point" ("creation_datetime", "student_id") VALUES (2012-03-28 09:38:41.516848, 1); args=[u'2012-03-28 09:38:41.516848', 1]
Out[8]: <Point: Point object>
In [9]: james.points
DEBUG (0.000) SELECT COUNT(*) FROM "testapp_point" WHERE "testapp_point"."student_id" = 1 ; args=(1,)
Out[9]: 2
I was not sure if you wanted to track the grade points. In this case, just add classname to the Point model.
Also note that Point.creation_datetime will be automatically set to the date and time the model was saved. I just posted a basic template that you can customize to suit your needs.
source to share