gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:51:37 26 4
gpt4 key购买 nike

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

我想展示这个模型。像这样enter image description here

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

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

我不知道该怎么做。没看懂Queryset,str,ORM...

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

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

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

模型.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.

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

管理员.py

from django.contrib import admin
from .models import Exam

admin.site.register(Exam)
# Register your models here.

最佳答案

我可能不理解你的应用程序,但如果你想在一个 View 中加载来自多个模型的数据,你可以重写基于通用类的 View 的 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

然后在您的 appname/templates/appname/pagename.html 中,您可以访问在 View 中查询的数据。在这种情况下,我们从模型 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 %}

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

关于Django - 一个 View 中的多个模型(表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59367543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com