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



OS - Windows10 蟒蛇 - 3.7.4 姜戈 - 2.1

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

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

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

我不知道该怎么做。我没有在 Queryset,str,ORM...

因为我刚刚启动 Django 3 个月。

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

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

Models.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.

Views.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.

admin.py

from django.contrib import admin
from .models import Exam
admin.site.register(Exam)
# Register your models here.

我可能不了解您的应用程序,但是如果您想在视图中加载来自多个模型的数据,则可以覆盖基于泛型类的视图的get_context_data方法。下面是使用模板视图的示例。

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

然后,在appname/templates/appname/pagename.html中,您可以访问在视图中查询的数据。在这种情况下,我们从模型 考试 和 法律文本 获取所有数据。您可以轻松地扩展它。

{% 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 %} 

好的,所以我看到您在我开始写答案后添加了您的观点...... 如果您使用基于函数的视图并使用 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})

最新更新