如何从两个不同的模型在Django中创建依赖下拉框?

时间:2018-08-12 09:53:07

标签: django django-models django-forms django-templates django-views

我想创建一个依赖项下拉列表以获取结果和学生表。

  • 从学生模型中,我必须在页面的下拉列表中获得可用的批次。

  • ,然后在第二个下拉列表中获取对应/取决于批次的可用课程。

  • 如果我点击“提交”,它将显示所有具有相同条件的学生的列表 在页面中的所选课程上批量处理和显示其结果

  • 列表项

我的models.py是:

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.validators import RegexValidator
from smart_selects.db_fields import ChainedForeignKey
class Course(models.Model):
    cid = models.AutoField(primary_key=True)
    cnam = models.CharField(max_length=200)
    cidn = models.IntegerField()
    cred = models.IntegerField()

    def __str__(self):
        return 'IT-' + str(self.cidn) + ' - ' + self.cnam

class Student(models.Model):
    snam = models.CharField(max_length=200)
    sid = models.AutoField(primary_key=True)
    sroll = models.IntegerField()
    sreg = models.IntegerField()
    sbtc = models.IntegerField()
    sses = models.CharField(max_length=10)

    def __str__(self):
        return self.snam

class Teacher(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    phone_regex = RegexValidator(regex=r'^\0?1?\d{11}$')
    mobile = models.CharField(validators=[phone_regex], max_length=11, blank=True)
    avatar = models.ImageField(upload_to='avater', blank=True)

    def __str__(self):
        return self.user.first_name + ' '+self.user.last_name

    @receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            Teacher.objects.create(user=instance)

    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        instance.teacher.save()

class Session(models.Model):
    sesid = models.IntegerField(primary_key=True,verbose_name= ('Session'))

    def __str__(self):
        return str(self.sesid)
    def get_absolute_url(self):
        return reverse('Dashboard:session')

class Registration(models.Model):
    session = models.ForeignKey(Session, on_delete=models.CASCADE)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, null=True)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)

    def get_absolute_url(self):
        return reverse("Dashboard:session")

    def __str__(self):
        return str(self.session) + " - " + self.course.cnam + " - " +self.teacher.user.first_name + ' '+self.teacher.user.last_name

class Result(models.Model):
    reg = models.ForeignKey(Registration, on_delete=models.CASCADE, null=True)
    student = models.ForeignKey(Student, on_delete=models.CASCADE, null=True)
    ct1 = models.FloatField(null=True, blank=True)
    ct2 = models.FloatField(null=True, blank=True)
    ct3 = models.FloatField(null=True, blank=True)
    asn = models.FloatField(null=True, blank=True)
    # avg
    atd = models.IntegerField(null=True, blank=True)
    #total
    def __str__(self):
        return str(self.reg.session) + ' - ' + 'IT-' + str(self.reg.course.cidn) + ' - ' + self.student.snam

0 个答案:

没有答案