如何在 django 中获取多对多关系表数据?

发布于 2025-01-10 02:09:11 字数 1057 浏览 3 评论 0原文

这是我的models.py,

from django.db import models

class Course(models.Model):
    Credits = (
        ('1', '0.75'),
        ('2', '1'),
        ('3', '2'),
        ('4', '3'),
        ('5', '4'),
        ('6', '5'),
        ('7', '6'),
    )

    course_code = models.CharField(max_length=20, default="CSE-101", unique=True)
    course_name = models.CharField(max_length=50, default="C Language", unique=True)
    course_credit = models.CharField(max_length=1, choices=Credits, default='4')

    def __str__(self):
        return self.course_code


class Student(models.Model):
    std_name = models.CharField(max_length=40)
    std_id = models.CharField(max_length=50, primary_key=True)
    std_email = models.EmailField(max_length=50, unique=True, blank=True)

    course = models.ManyToManyField(Course)

    def __str__(self):
        return self.std_id

当我在Student表中输入数据时,数据库创建一个表student_course,我想获取这个student_course数据。

我想获取每个学生注册了多少门课程以及每个课程名称的数据。我如何获取这些数据并在网页中显示这些数据?

我是 python 和 Django 的菜鸟,所以任何帮助将不胜感激。

This is my models.py

from django.db import models

class Course(models.Model):
    Credits = (
        ('1', '0.75'),
        ('2', '1'),
        ('3', '2'),
        ('4', '3'),
        ('5', '4'),
        ('6', '5'),
        ('7', '6'),
    )

    course_code = models.CharField(max_length=20, default="CSE-101", unique=True)
    course_name = models.CharField(max_length=50, default="C Language", unique=True)
    course_credit = models.CharField(max_length=1, choices=Credits, default='4')

    def __str__(self):
        return self.course_code


class Student(models.Model):
    std_name = models.CharField(max_length=40)
    std_id = models.CharField(max_length=50, primary_key=True)
    std_email = models.EmailField(max_length=50, unique=True, blank=True)

    course = models.ManyToManyField(Course)

    def __str__(self):
        return self.std_id

when i input data in Student table, database create a table student_course, i want to get this student_course data.

I want to get data each student how many course they registered and each course name. How can i get this data and show this data in webpage?

I'm a noob to python and Django so any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡尘雨 2025-01-17 02:09:11

我想获取此 Student_course 数据 ->要从学生表访问相关对象,您可以像这样访问 Student.course.all()


我想获取每个学生注册了多少门课程以及每门课程的数据名称 ->您可以通过过滤 Student 对象来检索每个学生的课程,如下

student = Student.objects.get(std_id=_place_student_id_here_)
courses = student.course.all() # this will return all courses related to perticular student

更新

如果您想为所有用户获取课程,您必须这样做,

students = Student.objects.all()
for student in students:
    print(student.course.all()) # this will return all courses for this student

如果您想在模板中重新渲染课程,请这样

views.py

def myview(request):
    students = Student.objects.all()
    return render(request, "studentlist.html",{'students':students})

studentlist.html 中的

{% for student in students %}
  <p>Student Name : {{student.std_name}}<p>
  <p>Courses : 
   {% for course in student.course.all %}
     <span>{{course.course_name}}</span>
   {% endfor %}
  </p>
{% endfor %}

i want to get this student_course data -> to access related object from student table you can access like this Student.course.all()


I want to get data each student how many course they registered and each course name -> You can retrieve courses of each student by filtering Student object like this

student = Student.objects.get(std_id=_place_student_id_here_)
courses = student.course.all() # this will return all courses related to perticular student

Update

If you want to get courses for all users you've to do like this

students = Student.objects.all()
for student in students:
    print(student.course.all()) # this will return all courses for this student

if you want to reder courses in template do like this

in you views.py

def myview(request):
    students = Student.objects.all()
    return render(request, "studentlist.html",{'students':students})

in your studentlist.html

{% for student in students %}
  <p>Student Name : {{student.std_name}}<p>
  <p>Courses : 
   {% for course in student.course.all %}
     <span>{{course.course_name}}</span>
   {% endfor %}
  </p>
{% endfor %}
趴在窗边数星星i 2025-01-17 02:09:11

您好,如果您想为每个学生提供课程,此代码可以帮助您:

from models import Student

my_student = Student.objects.get(std_id = {student id})
student_course = my_student.course
totoal_course = student_course.count()
name_of_courses = [course.name for course in student_course.all()]

hi if you want to get courser per student this code can help you:

from models import Student

my_student = Student.objects.get(std_id = {student id})
student_course = my_student.course
totoal_course = student_course.count()
name_of_courses = [course.name for course in student_course.all()]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文