如何在Django中制作依赖下拉列表

时间:2019-04-21 16:19:14

标签: django

我有3个模型,每3个模型是相互依赖的。当我选择学生姓名时通过表格添加Studentfee模型时,课程价格应该只与该学生的课程选择有关

  

models.py

from django.db import models
from django.db.models import CASCADE


class Course(models.Model):
    title = models.CharField(max_length=250)
    basic_price = models.CharField(max_length=100)
    advanced_price = models.CharField(max_length=100)
    basic_duration = models.CharField(max_length=50)
    advanced_duration = models.CharField(max_length=50)

    def __str__(self):
        return self.title


class Student(models.Model):
    name = models.CharField(max_length=100)
    courses = models.ManyToManyField(Course)
    address = models.CharField(max_length=200)
    email = models.EmailField()
    phone = models.CharField(max_length=15)
    image = models.ImageField(upload_to='Students',blank=True)
    joined_date = models.DateField()

    def __str__(self):
        return self.name


class StudentFee(models.Model):
    student = models.ForeignKey(Student,on_delete=CASCADE)
    total_fee = models.ForeignKey(Course,on_delete=CASCADE) # should dynamically generate in the form based on the selection of student.how ??
    first_installment = models.IntegerField(blank=True)
    second_installment = models.IntegerField(blank=True)
    third_installment = models.IntegerField(blank=True)
    remaining = models.IntegerField(blank=True)

    def __str__(self):
        return self.total_fee

1 个答案:

答案 0 :(得分:0)

我认为您正在处理有关应该计算什么和应该存储什么的问题。 我认为您将需要另一个模型来管理此问题,例如 CourseSelection,费用应在付款时计算,然后存储为CourseSelectionPayment