我试图获取学生记录的值,但根据条件(家长)更改检索到的值。
考虑Mault.Py:
class Grade(models.Model):
code_name = models.CharField(max_length=10)
school = models.ForeignKey(School,on_delete=models.CASCADE,blank=False,related_name='grades')
class Group(models.Model):
code_name = models.CharField(max_length=10)
grade = models.ForeignKey(Grade,on_delete=models.CASCADE,blank=False,related_name="groups")
student_record = models.OneToOneField(Student,null=True,blank=True,on_delete=models.SET_NULL)
class Class(models.Model):
code_name = models.CharField(max_length=10)
grade = models.ForeignKey(Grade,on_delete=models.CASCADE,blank=False,related_name="classes")
student_record = models.OneToOneField(Student,null=True,blank=True,on_delete=models.SET_NULL)
class Student(models.Model):
male_count = models.PositiveSmallIntegerField()
female_count = models.PositiveSmallIntegerField()
当我在如下表中检索学生记录时:
____________________________________________
| grade | class or group | males | females |
|_______|________________|_______|__________|
| | | | |
|_______|________________|_______|__________|
queryset将类似于以下内容:
rows = Student.objects.filter(<some condition>).values(
'<parent>__grade__codename',
'<parent> class or group', # here stands the issue
'male_count',
'female_count',
)
我试过很多概念。。比如使用case和when,搜索了几天,发现了这样一个未回答的问题,并试图通过为每个人(班级的学生、小组的学生)创建一个查询集来简化,但仍然无法实现。
任何支持都将不胜感激。
暂无答案!
目前还没有任何答案,快来回答吧!