Django ModelForm: How to Add ForeignKey as Field in Form?


Django ModelForm: How to Add ForeignKey as Field in Form?



I have Three Models StudentInfo, StdSubject, Marks


class StdSubject(StdCommon):
REGULAR='R'
OPTIONAL='O'

SUBJECT_TYPE_CHOICE=(
(REGULAR, 'REGULAR'),
(OPTIONAL, 'OPTIONAL')
)


subject_name = models.CharField('Subject Name', max_length=100)
subject_type = models.CharField(
'Subject Type', max_length=1, default=REGULAR, choices=SUBJECT_TYPE_CHOICE)
subject_full_marks = models.DecimalField(
'Full Marks', max_digits=5, decimal_places=2, default=100)
subject_pass_marks = models.DecimalField(
'Pass Marks', max_digits=5, decimal_places=2)


def __str__(self):
return self.subject_name


class StudentInfo(StdCommon):
STD_CLASS=(
('6','Six'),
('7','Seven'),
('8','Eight'),
('9','Nine'),
('10','Ten'),
)

STD_GENDER=(
('MALE','Male'),
('FEMALE','Female'),
)

STD_GROUP=(
('S', 'Science'),
('B', 'Business Studies'),
('H', 'Humatics'),
('G', 'General')
)

std_name = models.CharField('Student Name',max_length=100, help_text='Type only student Full Name like as Nazmul Islam or Nazrul Islam')
std_class = models.CharField('Student Class',max_length=2, choices=STD_CLASS, default=6, help_text='Select a class')
std_roll = models.IntegerField('Roll Number',help_text='Type Student Roll Number (Only Number)')
std_group=models.CharField('Group', choices=STD_GROUP, max_length=1, default='G')
std_gender=models.CharField('Gender', max_length=10, choices=STD_GENDER, default='MALE')
std_subjects = models.ManyToManyField(StdSubject)

def __str__(self):
return self.std_name





class Marks(StdCommon):
std_name=models.ForeignKey(StudentInfo, on_delete=models.CASCADE)
subject_name = models.ForeignKey(StdSubject, on_delete=models.CASCADE)
subject_marks=models.DecimalField(max_digits=5, decimal_places=2, help_text='Please give proper number')

subject_gradepoint=models.DecimalField('Grade Point', max_digits=3, decimal_places=1, blank=True, null=True, help_text="Please keep blank")
subject_gpa = models.CharField('Subject GPA', max_length=5, blank=True, null=True, help_text="Please keep blank")
std_result=models.FloatField(blank=True, null=True)



def __str__(self):
return self.std_name.std_name+' Class: '+str(self.std_name.std_class)+' Roll: '+str(self.std_name.std_roll) +' ' + self.subject_name.subject_name +' '+str(self.subject_marks)



def subject_grade(self):
grade = SubjectGrade(self.subject_marks,self.subject_name.subject_full_marks).subgrade()
return grade

def subject_grade_point(self):
grade = SubjectGradePoint( self.subject_marks, self.subject_name.subject_full_marks).subgrade()
return grade


def save(self, *args, **kwargs):
grade_point = SubjectGradePoint(self.subject_marks, self.subject_name.subject_full_marks).subgrade()
gpa = SubjectGrade(self.subject_marks,self.subject_name.subject_full_marks).subgrade()

if self.subject_name.subject_type=='O':
if self.subject_marks >= ((self.subject_name.subject_full_marks/100)*50) and self.subject_marks <= self.subject_name.subject_full_marks:
subject_opt_grade_point = grade_point-2

self.subject_gradepoint = subject_opt_grade_point
self.subject_gpa = gpa
elif self.subject_marks >= ((self.subject_name.subject_full_marks/100)*33) and self.subject_marks < ((self.subject_name.subject_full_marks/100)*50):
self.subject_gradepoint = 0
self.subject_gpa=gpa

elif self.subject_marks < ((self.subject_name.subject_full_marks/100)*33):
self.subject_gradepoint = 0
self.subject_gpa = gpa


elif self.subject_name.subject_type=='R':
self.subject_gradepoint = grade_point
self.subject_gpa = gpa

super().save(*args, **kwargs)



I created a Form like this


class StudentSubjectGPAFormAdd(forms.ModelForm):
class Meta:
model = Marks
exclude = ('std_name','subject_gpa',
'subject_gradepoint', 'std_result')

def __init__(self, *args, **kwargs):
super(IncomingForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['subject_marks'].queryset = StudentInfo.objects.filter(
std_name__exact=self.instance.id)



When I go to my student profile I want to add student subject marks. But It cant work. But It should be follow this lineup



Just I will select subject and input marks then save.



I want to add student marks like as this image
enter image description here









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV