Python 统计在公用列表中具有参数的实例数

Python 统计在公用列表中具有参数的实例数,python,django,django-rest-framework,Python,Django,Django Rest Framework,我正在开发一个应用程序来跟踪一所学校,并试图计算出每门课程有多少学生。我的应用程序的结构如下: class Course(models.Model): LEVELS = ( ('A0', 'A0'), ('A1', 'A1'), ('B0', 'B0'), ('B1', 'B1'), ('C1', 'C1'), ('C2', 'C2'), ) name = models.Cha

我正在开发一个应用程序来跟踪一所学校,并试图计算出每门课程有多少学生。我的应用程序的结构如下:

class Course(models.Model):
    LEVELS = (
        ('A0', 'A0'),
        ('A1', 'A1'),
        ('B0', 'B0'),
        ('B1', 'B1'),
        ('C1', 'C1'),
        ('C2', 'C2'),
    )
    name = models.CharField(max_length=32)
    level = models.CharField(max_length=2, choices=LEVELS)

class Student(models.Model):
    name = models.CharField(max_length=32)
    school = models.ForeignKey(School, on_delete=models.CASCADE)
    course = models.ManyToManyField(Course)
[
{
        "id": 1,
        "name": "Diego",
        "school": 2,
        "course": [8, 11]
    },
    {
        "id": 2,
        "name": "Garry",
        "school": 2,
        "course": [4]
    },
    {
        "id": 3,
        "name": "Dr Henrick",
        "school": 2,
        "course": [8]
    },
]
{ 8: 2, 11: 1, 4: 1}
因此,API中的学生看起来像这样:

class Course(models.Model):
    LEVELS = (
        ('A0', 'A0'),
        ('A1', 'A1'),
        ('B0', 'B0'),
        ('B1', 'B1'),
        ('C1', 'C1'),
        ('C2', 'C2'),
    )
    name = models.CharField(max_length=32)
    level = models.CharField(max_length=2, choices=LEVELS)

class Student(models.Model):
    name = models.CharField(max_length=32)
    school = models.ForeignKey(School, on_delete=models.CASCADE)
    course = models.ManyToManyField(Course)
[
{
        "id": 1,
        "name": "Diego",
        "school": 2,
        "course": [8, 11]
    },
    {
        "id": 2,
        "name": "Garry",
        "school": 2,
        "course": [4]
    },
    {
        "id": 3,
        "name": "Dr Henrick",
        "school": 2,
        "course": [8]
    },
]
{ 8: 2, 11: 1, 4: 1}
现在问题出现在我的序列化程序中,我想得到每门课程的计数,理想情况下是在dict结构中,它看起来像这样:

class Course(models.Model):
    LEVELS = (
        ('A0', 'A0'),
        ('A1', 'A1'),
        ('B0', 'B0'),
        ('B1', 'B1'),
        ('C1', 'C1'),
        ('C2', 'C2'),
    )
    name = models.CharField(max_length=32)
    level = models.CharField(max_length=2, choices=LEVELS)

class Student(models.Model):
    name = models.CharField(max_length=32)
    school = models.ForeignKey(School, on_delete=models.CASCADE)
    course = models.ManyToManyField(Course)
[
{
        "id": 1,
        "name": "Diego",
        "school": 2,
        "course": [8, 11]
    },
    {
        "id": 2,
        "name": "Garry",
        "school": 2,
        "course": [4]
    },
    {
        "id": 3,
        "name": "Dr Henrick",
        "school": 2,
        "course": [8]
    },
]
{ 8: 2, 11: 1, 4: 1}
我所做的工作包括从数据库中获取所有学生并循环查看他们的课程列表,但我想知道我是否可以使用Django的查询系统进行类似操作。

在提取所有课程信息后,您可以使用

from collections import Counter

students = [
    {
        "id": 1,
        "name": "Diego",
        "school": 2,
        "course": [8]
    },
    {
        "id": 2,
        "name": "Garry",
        "school": 2,
        "course": [1, 8]
    },
    {
        "id": 3,
        "name": "Dr Henrick",
        "school": 2,
        "course": [8]
    },
]

print(dict(Counter([course for y in [x["course"] for x in students] for course in y])))
输出:

{8: 3, 1: 1}   
您可以在提取所有课程信息后使用

from collections import Counter

students = [
    {
        "id": 1,
        "name": "Diego",
        "school": 2,
        "course": [8]
    },
    {
        "id": 2,
        "name": "Garry",
        "school": 2,
        "course": [1, 8]
    },
    {
        "id": 3,
        "name": "Dr Henrick",
        "school": 2,
        "course": [8]
    },
]

print(dict(Counter([course for y in [x["course"] for x in students] for course in y])))
输出:

{8: 3, 1: 1}   

Django的查询集有一个用于向结果添加聚合的功能。在本例中,您需要一个课程查询集,用于注释课程中的学生数量

from django.db.models import Count

courses = Course.objects.annotate(student_count=Count('students'))
这将为您提供一个queryset of Course实例,每个实例都有一个student_count属性。当然,如果你真的想把它变成字典。id:student\u count,你可以做:

{course.id: course.student_count for course in courses}

Django的查询集有一个用于向结果添加聚合的功能。在本例中,您需要一个课程查询集,用于注释课程中的学生数量

from django.db.models import Count

courses = Course.objects.annotate(student_count=Count('students'))
这将为您提供一个queryset of Course实例,每个实例都有一个student_count属性。当然,如果你真的想把它变成字典。id:student\u count,你可以做:

{course.id: course.student_count for course in courses}

如果一个学生参加了不止一门课程怎么办?e、 g.
“课程”:[8,2]
请参阅更新的答案如果一名学生参加了多个课程怎么办?e、 g.
“课程”:[8,2]
参见更新的答案