简体   繁体   English

传递几个参数以渲染模板

[英]Passing several arguments for rendering template

First of all, let me excuse for my English - it's not my native language. 首先,让我为我的英语找借口-这不是我的母语。

I have following models: 我有以下型号:

class Student(models.Model):
    student_id = models.IntegerField(primary_key=True)
    student_name = models.CharField(max_length=255, default='John Doe')
    dob = models.DateField(max_length=8)

class Group(models.Model):
    group_name = models.CharField(max_length=40)
    monitor = models.ForeignKey(Student)

class Student_Group(models.Model):
    student_id = models.ForeignKey(Student)
    group_name = models.ForeignKey(Group)

I need to render groups of students, its monitors and amount of students in each group. 我需要渲染一组学生,它的监视器和每组中的学生数量。 Making first two tasks is not a problem: 进行前两个任务不是问题:

views.py: views.py:

def group_list(request):
    groups = Group.objects.all()
    return render(request, 'groups/group_list.html', {'groups': groups})

groups.html groups.html

{% for group in groups %}
    <p>{{ group.group_name }}</p>
    <p>{{ group.monitor }}</p>
{% endfor %}

But when it comes to rendering amount of students for each group, I'm getting stuck. 但是当涉及到每个组的学生数量时,我陷入了困境。

Following SQL lets to count amount of students in given group 使用SQL可以计算给定组中的学生数量

select count(*) from students_student
join students_student_group
on students_student.student_id = students_student_group.student_id_id
where students_student_group.group_name_id = "Mega nerds"

Questions are: 问题是:

  1. How to get amount of students for each group using Django ORM instead, so the template will render following info: 如何使用Django ORM来获取每个组的学生数量,因此模板将呈现以下信息:

    • Group name: Mega nerds 团体名称:超级书呆子
    • Amount of students: 8 学生人数:8
    • Monitor: John Doe 班长:John Doe

    ... ...

    • Group name: Nice guys 小组名称:好家伙
    • Amount of students: 11 学生人数:11
    • Monitor: John Appleseed 班长:约翰·Appleseed
  2. How to pass data regarding amount of students to corresponding group. 如何将有关学生数量的数据传递给相应的组。

Thanks. 谢谢。

Update 更新资料

According to @Gocht advice, I used ManyToManyField, so my models.py now looks as 根据@Gocht的建议,我使用了ManyToManyField,所以我的models.py现在看起来像

class Student(models.Model):
    student_id = models.IntegerField(primary_key=True)
    student_name = models.CharField(max_length=255, default='Василий Пупкин')
    dob = models.DateField(max_length=8)

    def __str__(self):
        return self.student_name


class Group(models.Model):
    group_name = models.CharField(max_length=40, primary_key=True)
    monitor = models.ForeignKey(Student)
    students = models.ManyToManyField(Student, related_name='students')

also, as suggested, I've added decorator to Group class: 另外,如建议的那样,我已经将装饰器添加到Group类中:

@property
def get_students_qty(self):
    return self.students.all().count()

so now I can get number of students in each group, like so: 所以现在我可以得到每个组的学生人数,如下所示:

{% for group in groups %}
    <p>{{ group.group_name }}</p>
    <p>{{ group.monitor }}</p>
    <p>{{ group.get_students_qty }}</p>
{% endfor %}

But I still wondering - is it possible to get number of students in group without using decorator? 但是我仍然想知道-是否可以在不使用装饰器的情况下获得小组中的学生人数? After all, Group class has students field... 毕竟, Group课有students领域...

You could get the number of students in a group like this: 您可以像这样获得一组学生的数量:

group = ...  # get a group

n_students = Student_Group.objects.filter(group=group).count()

Then since every Student_Group object has one student, n_students will contain the number of student in the given group. 然后,由于每个Student_Group对象都有一个学生,因此n_students将包含给定组中的学生人数。

To send this number to your template you can add it in your context: 要将这个号码发送到您的模板,您可以在上下文中添加它:

def group_list(request):
    groups = Group.objects.all()
    return render(request, 'groups/group_list.html', {'groups': groups, 'n_students': n_students})

You could also see docs for ManyToMany relationships, that could be helpful here. 您还可以查看有关ManyToMany关系的文档,这可能对您有所帮助。

EDIT 编辑

Take some time to check Python's naming conventions ; 花一些时间检查Python的命名约定 your Student_Group should be StudentGroup . Student_Group应该是StudentGroup

You can create a method in your model to return the number of students in a group: 您可以在模型中创建一个方法来返回组中的学生人数:

# models.py
class Group(models.Model):

    # fields

    @property
    def get_students_qty(self):
        return self.student_group_set.all().count()
        # Try with self.studentgroup_set.all().count() if the line
        # above does not work

then in your template: 然后在您的模板中:

{% for group in groups %}
    <p>{{ group.group_name }}</p>
    <p>{{ group.monitor }}</p>
    <p>{{ group.get_students_qty }}</p>
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM