Django限制在选择后以形式选择

时间:2017-01-04 15:06:20

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

我希望在选择前一个字段后限制表单中给出的选项。 这是我的models.py文件:

class Course(models.Model):
name = models.CharField(max_length=70)
course_id = models.CharField(max_length=10, default="")
major = models.CharField(
    max_length=45,
    choices= (
    ('bachelorcollege', 'Bachelor College'),
    ('appliedmathematics', 'Applied Mathematics'),
    ('appliedphysics', 'Applied Physics'),
    ('architecture', 'Architecture, Urbanism and Building Sciences'),
    ('automotive', 'Automotive'),
    ('biomedicalengineering', 'Biomedical Engineering'),
    ('chemicalengineering', 'Chemical Engineering'),
    ('computerscience', 'Computer Science'),
    ('electricalengineering', 'Electrical Engineering'),
    ('industrialdesign', 'Industrial Design'),
    ('industrialengineering', 'Industrial Engineering'),
    ('mechanicalengineering', 'Mechanical Engineering'),
    ('psychologyandtechnology', 'Psychology and Technology'),
    ('sustainableinnovation', 'Sustainable Innovation'),
    ),
    default='computerscience',
    )
def __str__(self):
    return self.name

def __major__(self):
    return self.major


class Resource(models.Model):
uploadedBy = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
resourcefile = models.FileField(blank=False, default="")
major = models.CharField(
    max_length=45,
    choices= (
    ('bachelorcollege', 'Bachelor College'),
    ('appliedmathematics', 'Applied Mathematics'),
    ('appliedphysics', 'Applied Physics'),
    ('architecture', 'Architecture, Urbanism and Building Sciences'),
    ('automotive', 'Automotive'),
    ('biomedicalengineering', 'Biomedical Engineering'),
    ('chemicalengineering', 'Chemical Engineering'),
    ('computerscience', 'Computer Science'),
    ('electricalengineering', 'Electrical Engineering'),
    ('industrialdesign', 'Industrial Design'),
    ('industrialengineering', 'Industrial Engineering'),
    ('mechanicalengineering', 'Mechanical Engineering'),
    ('psychologyandtechnology', 'Psychology and Technology'),
    ('sustainableinnovation', 'Sustainable Innovation'),
    ),
    #default='computerscience',
    )

course = models.ForeignKey(Course)
resourcetype = models.CharField(
    max_length=45,
    choices= (
    ('Summary', 'Summary'),
    ('Exam', 'Exam'),
    ),
    default='Summary',
    )
upload_date = models.DateTimeField(
        blank=True, null=True)

def __str__(self):
    return self.title

这是我的forms.py文件:

class UploadForm(forms.ModelForm):

class Meta:
    model = Resource
    fields = ('title', 'major', 'course', 'resourcetype', 'resourcefile')


class AddCourseForm(forms.ModelForm):

class Meta:
    model = Course
    fields = ('name', 'course_id', 'major')

现在当我在模板中加载UploadForm时,您可以选择一个专业,然后选择一个课程。但是在选择专业时,我想限制属于该专业的课程(我现在获得所有课程)。因此,如果我选择biomedicalengineering,我想将课程中的选择限制为仅属于该专业的课程。任何关于我如何建模数据库的评论也是受欢迎的。

背景信息:我正在尝试开发一个网站,用户可以上传摘要和考试(属于特定课程,属于特定专业)

0 个答案:

没有答案