获取错误'ClassAssignment' object has no attribute 'assignmentfileupload'
Getting the error 'ClassAssignment' object has no attribute 'assignmentfileupload'
我使用的模型如下
class ClassAssignment(models.Model) :
classroom = models.ForeignKey(Classroom, on_delete=models.CASCADE)
title = models.CharField(max_length=300, blank=False, null=False)
instructions = models.TextField(blank=True, null=True)
completed = models.ManyToManyField(User)
due_date = models.DateTimeField(blank=True, null=True)
created_on = models.DateTimeField(default=datetime.now())
def __str__(self) -> str :
return self.title
class AssignmentFileUpload(models.Model) :
assignment = models.ForeignKey(ClassAssignment, on_delete=models.CASCADE)
attachment = models.FileField(upload_to="assignment_uploads/")
created_on = models.DateTimeField(default=datetime.now())
def __str__(self) -> str :
return self.assignment.title
def delete(self, *args, **kwargs) :
self.attachment.delete(save=False)
return super().delete(*args, **kwargs)
序列化程序的代码
class AssignmentSerializer(serializers.ModelSerializer) :
assignmentfileupload = serializers.HyperlinkedRelatedField(
view_name='file_uploads',
many=True,
read_only = True
)
class Meta :
model = ClassAssignment
fields = (
'id',
'classroom',
'title',
'instructions',
'due_date',
'created_on',
'assignmentfileupload'
)
我不明白我哪里做错了。甚至文档也说了同样的话,我照做了,但它没有按预期工作。
我进行了这些更改,现在可以正常工作 有人可以解释一下之前出了什么问题吗
Screenshot of the code
根据文档
view_name - The view name that should be used as the target of the relationship. If you're using the standard router classes this will be a string with the format -detail. required.
所以你必须提供 view_name
作为 AssignmentFileUpload-detail
我使用的模型如下
class ClassAssignment(models.Model) :
classroom = models.ForeignKey(Classroom, on_delete=models.CASCADE)
title = models.CharField(max_length=300, blank=False, null=False)
instructions = models.TextField(blank=True, null=True)
completed = models.ManyToManyField(User)
due_date = models.DateTimeField(blank=True, null=True)
created_on = models.DateTimeField(default=datetime.now())
def __str__(self) -> str :
return self.title
class AssignmentFileUpload(models.Model) :
assignment = models.ForeignKey(ClassAssignment, on_delete=models.CASCADE)
attachment = models.FileField(upload_to="assignment_uploads/")
created_on = models.DateTimeField(default=datetime.now())
def __str__(self) -> str :
return self.assignment.title
def delete(self, *args, **kwargs) :
self.attachment.delete(save=False)
return super().delete(*args, **kwargs)
序列化程序的代码
class AssignmentSerializer(serializers.ModelSerializer) :
assignmentfileupload = serializers.HyperlinkedRelatedField(
view_name='file_uploads',
many=True,
read_only = True
)
class Meta :
model = ClassAssignment
fields = (
'id',
'classroom',
'title',
'instructions',
'due_date',
'created_on',
'assignmentfileupload'
)
我不明白我哪里做错了。甚至文档也说了同样的话,我照做了,但它没有按预期工作。
我进行了这些更改,现在可以正常工作 有人可以解释一下之前出了什么问题吗 Screenshot of the code
根据文档
view_name - The view name that should be used as the target of the relationship. If you're using the standard router classes this will be a string with the format -detail. required.
所以你必须提供 view_name
作为 AssignmentFileUpload-detail