我正在使用自动结果系统应用程序。我在models.py文件中有五个模型类。
class Student(models.Model):
std_name=models.CharField('Student Name ', max_length=200)
CLASS_NAME=(
('6','SIX'),
('7','SEVEN'),
('8','Eight'),
('9','Nine'),
('10','Ten'),
('Ex-10','EX-Ten'),
)
std_class_name=models.CharField('Class Name', max_length=7, choices=CLASS_NAME)
std_roll_number=models.IntegerField('Roll Number')
GENDER=(
('Male','Male'),
('Female','Female'),
)
std_gender=models.CharField('Gender', max_length=7, choices=GENDER)
GROUP=(
('Science','Science Group'),
('B.Studies','Business Group'),
('Arts and Humatics','Arts and Humatics'),
('General','General'),
)
std_group=models.CharField('Group', max_length=17, choices=GROUP)
pub_date = models.DateTimeField('Published', auto_now_add=True)
std_total_subject=models.IntegerField('Total Subject', default=0)
def __str__(self):
return self.std_name
class ExaminationName(models.Model):
examination_name=models.CharField('Examination Name ', max_length=100)
exam_date=models.DateTimeField('Exam Date: ')
pub_date = models.DateTimeField('Published', auto_now_add=True)
def __str__(self):
return self.examination_name
class MainSubject(models.Model):
main_subject_name = models.CharField('Main Subject Name', max_length=100)
main_subject_code=models.DecimalField('Main Subject Code', decimal_places=0, default=0, max_digits=10)
subject_full_marks = models.DecimalField('Subject Full Marks', max_digits=6, decimal_places=0, default=100)
pub_date = models.DateTimeField('Published', auto_now_add=True)
def __str__(self):
return self.main_subject_name
class SubjectName(models.Model):
mainsubject = models.ForeignKey(MainSubject, on_delete=models.CASCADE)
subject_name=models.CharField('Subject Name', max_length=100)
subject_full_marks=models.DecimalField('Subject Full Marks',max_digits=6, decimal_places=0, default=100)
pub_date = models.DateTimeField('Published', auto_now_add=True)
subject_gpa_point=models.DecimalField('GPA in Subject',max_digits=4, decimal_places=2, default=0)
subject_gpa_grade=models.CharField('GPA Grade',max_length=5, default='F')
def __str__(self):
return self.subject_name
class SubjectPart(models.Model):
mainsubject = models.ForeignKey(MainSubject, on_delete=models.CASCADE)
subjectname = models.ForeignKey(SubjectName, on_delete=models.CASCADE)
subject_part_name=models.CharField('Subject Part', max_length=100)
subject_part_full_marks = models.DecimalField('Full Marks', max_digits=6, decimal_places=0, default=100)
subject_part_pass_marks = models.DecimalField('Pass Marks', max_digits=6, decimal_places=0, default=100)
pub_date = models.DateTimeField('Published', auto_now_add=True)
def __str__(self):
return self.subject_part_name
class AddResult(models.Model):
student=models.ForeignKey(Student, on_delete=models.CASCADE)
examinationname = models.ForeignKey(ExaminationName, on_delete=models.CASCADE)
mainsubject_name = models.ForeignKey(MainSubject, on_delete=models.CASCADE)
subjectname = models.ForeignKey(SubjectName, on_delete=models.CASCADE)
subjectpart = models.ForeignKey(SubjectPart, on_delete=models.CASCADE)
subject_number=models.DecimalField('Subject Number', max_digits=6, decimal_places=0)
pub_date = models.DateTimeField('Published', auto_now_add=True)
def __str__(self):
return str(self.subjectpart)
def fail_sub(self):
if self.subject_number < self.subjectpart.subject_part_pass_marks:
return 'F'
我已经通过Django管理面板创建了一个MainSubject对象。我通过foreignkey在SubjectName Model类中添加了主题。 我在SubjectName模型类中创建了两个外键。我通过Django-admin面板在SubjectPart模型中创建了任何对象。
然后我创建了一个AddResulForm ModelForm。但问题是当我选择主题名称时,所有主题都显示时间。如果我选择Bangla作为主题,那么我只选择主题名称部分中的Bangla First Part或Bangla 2nd Part。但是现在以这种形式展示了所有主题。 如何在Django表单中实现我的想法?
答案 0 :(得分:1)
,您应该在admin.py
中编写方法您可以为模型创建一个类(主题部分)并为该字段实现一个方法。
您可以参考此https://docs.djangoproject.com/en/1.11/ref/contrib/admin/
答案 1 :(得分:0)
在Django中你可以过滤选择集,但是前端交互,你可能需要重新实现表单模板html代码或表单字段。