The models that I am using are as followed
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)
The code for the serializer
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'
)
I am not able to understand where I am doing wrong. Even the documentation says the same thing and I followed it but it is not working as expected.
I made these changes and now It is working Can someone please explain what went wrong earlier Screenshot of the code
CodePudding user response:
According to docs
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.
So you have to provide view_name
as AssignmentFileUpload-detail