class Grade(models.Model):
name = models.CharField(u'年级', max_length=64)
school = models.ForeignKey('School', verbose_name=u'学校')
def __unicode__(self):
return self.name
class Class(models.Model):
name = models.CharField(u'班级', max_length=64)
grade = models.ForeignKey('Grade', verbose_name=u'年级', related_name='grade')
def getGradeName(self):
return self.grade.name
def __unicode__(self):
return self.name
class Student(models.Model):
name = models.CharField(u'姓名', max_length=64)
sex = models.CharField(u'性别', max_length=64)
id_num = models.CharField(u'身份证号', max_length=64)
student_num = models.CharField(u'学号', max_length=64)
class_id = models.ForeignKey('Class', verbose_name=u'班级', related_name='class_id')
grade = class_id.name
def __unicode__(self):
return self.name
我想让学生上课,然后将其分配到模范学生的成绩。
如何在模特学生中获得成绩?
答案 0 :(得分:1)
由于grade
将是Class
Student
实例的grade
,我建议您从Student
删除Student
类变量,而不是添加property到班级class Student(models.Model):
name = models.CharField(u'姓名', max_length=64)
sex = models.CharField(u'性别', max_length=64)
id_num = models.CharField(u'身份证号', max_length=64)
student_num = models.CharField(u'学号', max_length=64)
class_id = models.ForeignKey('Class', verbose_name=u'班级', related_name='class_id')
def __unicode__(self):
return self.name
@property
def grade_name(self):
return self.class.grade.name
。像这样。
grade
要获取Student
实例的student = Student.object.all()[0]
grade = student.grade_name
,它将是
services:
user.default:
class: \UserBundle\DefaultController
答案 1 :(得分:0)
#get first student in table
s = Student.object.all()[0]
# get Grade access through Class model
print s.class_id.grade