Django - 一个视图中的多个模型(表)

You*_*ung 2 django django-templates django-models django-forms django-views

操作系统 - Windows10 Python - 3.7.4 Django - 2.1

我想展示这个模型。像这样在这里输入图片描述

但我不知道。我怎样才能做到这一点。我试图解决这个问题 3 个月 :( 我已经有了 MySQL 数据库。所以,我必须保留这些数据结构(模型)

管理继承?覆盖 Django Admin 中的查询集?

我不知道该怎么做。我不明白 Queryset,str,ORM...

因为我刚开始使用 Django 3 个月。

我必须解决这个问题。一个视图中的多个模型(表==类)(模板)

但我不能.. 请告诉我提示,解决方案,任何事情

模型.py

from django.conf import settings
from django.db import models
from django.utils import timezone

class Exam(models.Model):
    idexam = models.IntegerField(primary_key=True)
    trueanswernum = models.IntegerField(db_column='trueAnswerNum', blank=True, null=True)  # Field name made lowercase.
    falseanswernum = models.IntegerField(db_column='falseAnswerNum', blank=True, null=True)  # Field name made lowercase.
    exammain = models.TextField(db_column='examMain', blank=True, null=True)  # Field name made lowercase.
    questionsubmain = models.TextField(db_column='questionSubMain', blank=True, null=True)  # Field name made lowercase.
    answerno1 = models.TextField(db_column='answerNo1', blank=True, null=True)  # Field name made lowercase.
    answerno2 = models.TextField(db_column='answerNo2', blank=True, null=True)  # Field name made lowercase.
    answerno3 = models.TextField(db_column='answerNo3', blank=True, null=True)  # Field name made lowercase.
    answerno4 = models.TextField(db_column='answerNo4', blank=True, null=True)  # Field name made lowercase.
    rightanswer = models.IntegerField(db_column='rightAnswer', blank=True, null=True)  # Field name made lowercase.
    originalremark = models.TextField(db_column='originalRemark', blank=True, null=True)  # Field name made lowercase.
    writer = models.CharField(max_length=45, blank=True, null=True)
    writingdate = models.DateTimeField(db_column='writingDate', blank=True, null=True, auto_now_add=True)  # Field name made lowercase.
    sources = models.CharField(max_length=45, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'boards_exam'

    def __str__(self):
        return self.exammain

class Examhistory(Exam):
    idexamhistory = models.AutoField(db_column='idExamhistory', primary_key=True)  # Field name made lowercase.
    idexam = models.IntegerField()
    examyear = models.IntegerField(db_column='examYear')  # Field name made lowercase.
    examtypecode = models.IntegerField(db_column='examTypeCode')  # Field name made lowercase.
    yearexamnum = models.IntegerField(db_column='yearExamNum', blank=True, null=True)  # Field name made lowercase.
    examsubj = models.IntegerField(db_column='examSubj', blank=True, null=True)  # Field name made lowercase.
    examnum = models.IntegerField(db_column='examNum', blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'boards_examhistory'
        unique_together = (('idexamhistory', 'idexam'),)


class Examtype(models.Model):
    examtypecode = models.IntegerField(db_column='examTypeCode', primary_key=True)  # Field name made lowercase.
    examtypename = models.CharField(db_column='examTypeName', max_length=20, blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'boards_examtype'


class Lawinfo(models.Model):
    lawnamecode = models.IntegerField(db_column='lawNameCode', primary_key=True)  # Field name made lowercase.
    lawname = models.CharField(db_column='lawName', max_length=25, blank=True, null=True)  # Field name made lowercase.
    lawadmin = models.CharField(db_column='lawAdmin', max_length=15, blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'boards_lawinfo'


class Lawtext(models.Model):
    idlawtext = models.AutoField(primary_key=True)
    lawnamecode = models.IntegerField(db_column='lawNameCode', blank=True, null=True)  # Field name made lowercase.
    lawcategory = models.IntegerField(db_column='lawCategory', blank=True, null=True)  # Field name made lowercase.
    lawcontent_jo = models.CharField(db_column='lawContent_jo', max_length=5, blank=True, null=True)  # Field name made lowercase.
    lawcontent_hang = models.CharField(db_column='lawContent_hang', max_length=5, blank=True, null=True)  # Field name made lowercase.
    lawcontent_ho = models.CharField(db_column='lawContent_ho', max_length=5, blank=True, null=True)  # Field name made lowercase.
    lawcontent_mok = models.CharField(db_column='lawContent_mok', max_length=5, blank=True, null=True)  # Field name made lowercase.
    lawtext = models.TextField(db_column='lawText', blank=True, null=True)  # Field name made lowercase.
    update_date = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'boards_lawtext'


class Relatedlaw(models.Model):
    idrelatedlaw = models.AutoField(primary_key=True)
    idexam = models.IntegerField()
    lawnamecode = models.IntegerField(db_column='lawNameCode', blank=True, null=True)  # Field name made lowercase.
    lawcategory = models.CharField(db_column='lawCategory', max_length=5, blank=True, null=True)  # Field name made lowercase.
    lawcontent_jo = models.CharField(db_column='lawContent_jo', max_length=5, blank=True, null=True)  # Field name made lowercase.
    lawcontent_hang = models.CharField(db_column='lawContent_hang', max_length=5, blank=True, null=True)  # Field name made lowercase.
    lawcontent_ho = models.CharField(db_column='lawContent_ho', max_length=5, blank=True, null=True)  # Field name made lowercase.
    lawcontent_mok = models.CharField(db_column='lawContent_mok', max_length=5, blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'boards_relatedlaw'
        unique_together = (('idrelatedlaw', 'idexam'),)

# Create your models here.
Run Code Online (Sandbox Code Playgroud)

视图.py

from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Exam

def exam_list(request):
    exams = Exam.objects.filter(writingdate__lte=timezone.now()).order_by('writingdate')
    return render(request, 'boards/exam_list.html', {'exams': exams})

def exam_detail(request, pk):
    exam = get_object_or_404(Exam, pk=pk)
    return render(request, 'boards/exam_detail.html', {'exam': exam})
# Create your views here.
Run Code Online (Sandbox Code Playgroud)

管理文件

from django.contrib import admin
from .models import Exam

admin.site.register(Exam)
# Register your models here.
Run Code Online (Sandbox Code Playgroud)

fil*_*phl 5

我可能不了解您的应用程序,但是如果您想从视图中的多个模型加载数据,您可以覆盖get_context_data基于通用类的视图的方法。这是一个使用TemplateView的示例。

from django.views.generic.base import TemplateView
from .models import Exam, Lawtext


class PageView(TemplateView):

    template_name = "pagename.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['exams'] = Exam.objects.all()
        context['lawtexts'] = Lawtext.objects.all()
        return context
Run Code Online (Sandbox Code Playgroud)

然后,您appname/templates/appname/pagename.html可以访问您在视图中查询的数据。在这种情况下,我们从模型 Exam 和 Lawtext 中获取所有数据。你可以很容易地扩展它。

{% for exam in exams %}
    <h4>{{exam.exammain}} </h4>
    <p>{{exam.rightanswer}}</p>
{% endfor %} 

{% for lawtext in lawtexts %}
    <p>{{lawtext.lawnamecode}}</p>
    <p>{{lawtext.lawcategory}}</p>
{% endfor %} 
Run Code Online (Sandbox Code Playgroud)

好的,所以我看到你在我开始写我的答案后添加了你的视图......如果你使用基于函数的视图并使用render()返回,你可以用类似的方式来做到这一点。您的 views.py 可能如下所示:

from .models import Exam, Lawtext

def exam_list(request):
    exams = Exam.objects.filter(writingdate__lte=timezone.now()).order_by('writingdate')
    lawtexts = Lawtext.objects.all()
    return render(request, 'boards/exam_list.html', {'exams': exams, 'lawtexts': lawtexts})
Run Code Online (Sandbox Code Playgroud)