如何从同一表的许多Foreignkey字段中获取相同的数据?

时间:2019-02-07 05:39:17

标签: python django

我是Django的新手,并且正在开发一个教练网站,我想通过外键将studentID和多个CourseId存储在学生课程表中,并根据studentID并在单列的“ HTML TABLE”页面上显示学生数据和学生选择的所有课程。

Model.py

class course_content(models.Model):
      course = models.CharField(max_length= 100)
      Code = models.CharField(max_length=50)

class student_admission(models.Model):
      Student = models.CharField(max_length= 50)
      Father = models.CharField(max_length= 50)
      Date = models.CharField(max_length= 50)
      Gender = models.CharField(max_length= 50)

class student_course(models.Model):
     Student_id= models.ForeignKey(student_admission,on_delete=None)
     Course_id = models.ForeignKey(course_content,on_delete=None)   

Views.py

def savedata(request):
    student = request.POST.get("student")
    father = request.POST.get("father")
    date = request.POST.get("date")
    gender = request.POST.get("gender")
    c_id = request.POST.getlist("cid")
    obj = student_admission(Student=student,Father=father,Date=date,Gender=gender)

    obj.save()
    for id in c_id:
        obj2 = student_course(Student_id_id=obj.id,Course_id_id=id)
        obj2.save()
    return HttpResponseRedirect(settings.BASEURL +'/home')


def table(request):
    data = student_course.objects.all().prefetch_related('Student_id','Course_id')
    unique_list = [] 
    for x in data:
        if x not in unique_list:
           unique_list.append(x) 
    d = {'data1' : unique_list}
    return render(request,"html/tables.html",d)

Table.html

        {% for c in data1 %}
        <tr>
        <td>{{c.Student_id.id}}</td>
        <td>{{c.Student_id.Student}}</td>
        <td>{{c.Student_id.Father}}</td>
        <td>{{c.Student_id.Date}}</td>
        <td>{{c.Course_id.course }}</td>
        <td>{{c.Student_id.Gender}}</td>
        <td><a type="button" class="btn btn-success" href="../editid/{{item.id}}">Edit</a>&nbsp;<a type="button" class="btn btn-success" href="../deleteid/{{item.id}}">Delete</a></td>
        </tr>
        {% endfor %}

2 个答案:

答案 0 :(得分:0)

在models.py中仅制作2个这样的模型

constraints: BoxConstraints(maxHeight: 300),

现在查找学生所修的所有课程: 假设您有一个学生s = get_object_or_404(Student,id = 1)

all_courses = s.course_set.all()

将包含id = 1的学生修读的每门课程。

您可以遍历模板中的所有课程以进行打印。

class Course(models.Model):
      course = models.CharField(max_length= 100)
      code = models.CharField(max_length=50)


      def __str__(self):
          return self.course




GENDER = (
    ('M', 'M'),
    ('F', 'F'),
)
class Student(models.Model):
      name = models.CharField(max_length= 50)
      course  = models.ManyToManyField(Course)
      father = models.CharField(max_length= 50)
      date = models.DateField(max_length= 50)
      gender = models.CharField(max_length= 50, choices = GENDER)

      def __str__(self):
          return self.name

答案 1 :(得分:-1)

您可以使用ManyToManyField而不是ForeignKey来实现目标。

Course_id = models.ManyToManyField(course_content)

此外,请查看此链接以获取有关ManyToManyField的详细信息。 https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_many/