在我正在开发的Django项目中,有一个名为Course
的模型,它包含许多Assignment
对象。
这是models.py
的一部分:
class Assignment(models.Model):
course = models.ForeignKey(Course, related_name='get_assignments')
name = models.CharField(max_length=200)
ipaddress = models.CharField(null=True, max_length=500)
publish_type = models.CharField(max_length=50, default="Scheduled")
type_of_lab = models.CharField(max_length=50, default="Lab")
timeduration = models.DurationField(default=timedelta(seconds=0), null=True)
late_duration = models.DurationField(default=timedelta(seconds=0), null=True)
exam_group_id = models.CharField(max_length=50, null=True)
serial_number = models.IntegerField()
program_language = models.CharField(max_length=32)
duration = models.DurationField(default=timedelta(seconds=0), null=True)
freezing_duration = models.DurationField(default=timedelta(seconds=0), null=True)
deadline = models.DateTimeField(null=True)
freezing_deadline = models.DateTimeField(null=True)
publish_on = models.DateTimeField(null='true')
bulk_add = models.FileField(max_length = 800,upload_to=assignment_bulk_upload_path, null=True)
通过表单创建分配时,将使用某些功能来验证表单。这是forms.py
的一部分:
class AssignmentForm(forms.Form):
assignment_type = forms.BooleanField(required=False, widget=forms.HiddenInput())
name = forms.CharField(label="Assignment Name", widget=forms.TextInput(attrs={'placeholder':'Assignment Name (200 Characters or fewer)','maxlength':200}))
Typeof = (('Lab', 'Lab'), ('Exam', 'Exam'))
publishtype = (('Scheduled', 'Scheduled'), ('On Demand', 'On Demand'))
publish_type = forms.ChoiceField(
choices=publishtype,
label="Choose type of Publish (Default : Scheduled)"
)
duration = forms.DurationField(
label="Assignment Duration",
required=False,
help_text="Assignment Duration, input format is HH:MM:SS "
)
freezing_duration = forms.DurationField(
label="Freezing extra time",
required=False,
help_text="Extra time for students to freeze submission, input format is HH:MM:SS "
)
bulk_add = forms.FileField(
error_messages={'invalid': 'File was not a valid tar file.'},
required=False,
label="Add sections and/or test cases in bulk",
)
#
#
# some more code
#
#
def clean_bulk_add(self):
assig1 = self.cleaned_data['name']
course1 = self.this_course
user1 = course1.owner
tot1 = 30 + len(str(user1)) + len(str(course1.title)) + len(str(assig1)) + len(str(self.cleaned_data['bulk_add']))
if tot1 > 700 :
raise forms.ValidationError("Assignment or Course name is too long: "+str(tot1))
return self.cleaned_data['bulk_add']
def clean_name(self):
if not hasattr(self, 'this_course'):
return self.cleaned_data['name']
try:
_ =Assignment.objects.filter(trash=False).get(name=self.cleaned_data['name'], course=self.this_course)
raise forms.ValidationError('This assignment already exists in this course.')
except Assignment.DoesNotExist:
pass
return self.cleaned_data['name']
正如您在验证AssignmentForm
时所看到的,我想使用创建它的课程名称。因此,在函数clean_bulk_add
中,我编写了course1 = self.this_course
。
然后显示AttributeError 'AssignmentForm' object has no attribute 'this_course'
。那么,如何获得要在其中创建作业的课程的名称?