如何在 Django 中获取多对多关系 table 数据?
How can i get many to many relationship table data in django?
这是我的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 table 中输入数据时,数据库创建了一个 table student_course,我想得到这个 student_course 数据。
我想获取每个学生注册了多少门课程以及每个课程名称的数据。如何获取这些数据并在网页中显示这些数据?
我是 python 和 Django 的新手,因此非常感谢任何帮助。
i want to get this student_course data
-> 要访问学生 table 的相关对象,您可以像这样访问 Student.course.all()
I want to get data each student how many course they registered and each course name
-> 您可以通过过滤 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 %}
你好,如果你想为每个学生获得课程,此代码可以帮助你:
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()]
这是我的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 table 中输入数据时,数据库创建了一个 table student_course,我想得到这个 student_course 数据。
我想获取每个学生注册了多少门课程以及每个课程名称的数据。如何获取这些数据并在网页中显示这些数据?
我是 python 和 Django 的新手,因此非常感谢任何帮助。
i want to get this student_course data
-> 要访问学生 table 的相关对象,您可以像这样访问 Student.course.all()
I want to get data each student how many course they registered and each course name
-> 您可以通过过滤 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 %}
你好,如果你想为每个学生获得课程,此代码可以帮助你:
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()]