Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - multiple models(table) in one view

OS - Windows10 Python - 3.7.4 Django - 2.1

I want to show this models. like thisenter image description here

but i don't know. How can i do that. I tried to solve this problem for 3months :( already I have MySQL Database. So, I have to stay these Data Structure(Models)

Admin Inheritance? Override Queryset in Django Admin?

I don't know how to do that. I didn't under stand Queryset,str,ORM...

Because I just have started Django for 3months.

I have to fix this problem. Multiple models(table==class) in one view(templates)

But I can't.. Please Tell me the hint, Solution, anything

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.
like image 835
Young Avatar asked Dec 14 '25 02:12

Young


1 Answers

I might not understand your application, but if you want to load data from multiple models in a view, you can override the get_context_data method of the generic class based views. Here is an example using 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

Then in your appname/templates/appname/pagename.html you have access to the data you queried in the view. In this case we get all the data from the models Exam and Lawtext. You could easily extend this.

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

OK, so I see you added added your views after I started writing my answer... If you use function based views and return using render(), you can do this in a similar way. Your views.py can look something like this:

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})
like image 53
filiphl Avatar answered Dec 16 '25 22:12

filiphl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!