I am having following Models in my project:
Site: site = models.CharField(max_length=30, blank=False, null=False)
approver = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="design_site_approver", blank=True, null=True, limit_choices_to={'is_approver': True})
submitter = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="design_site_submitter", blank=True, null=True)
Comment: user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_name_comment')
timestamp = models.DateTimeField(default=datetime.datetime.now)
remarks = models.TextField(null=True, blank=True)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='replies')
site = models.ForeignKey(InDesignSite, on_delete=models.CASCADE, related_name='site_name_comment')
File: site = models.ForeignKey(InDesignSite, on_delete=models.CASCADE, related_name='site_name')
def upload_to_path(self, filename):
site = self.site.site
return f'/{site}/{filename}'
attachment = models.FileField(upload_to=upload_to_path)
Following is my serializer file:
Comment: class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = '__all__'
Files: class FileSerializer(serializers.ModelSerializer):
class Meta:
model = Files
fields = '__all__'
Site: class SiteSerializer(serializers.ModelSerializer):
remarks = CommentSerializer(many=True,read_only=True)
attachment = FileSerializer(many=True,read_only=True)
class Meta:
model = Site
fields = ['id','site', 'project_pm', 'vendor', 'operator', 'market', 'approver', 'submitter', 'create_on', 'status', 'site_type', 'remarks', 'attachment']
I need to get the Comments made by a particular user on a particular site and the attached files. However, I am unable to get them, and I am getting no error as well. When I tried accessing SiteSerializer from Comment Serializer it worked fine, however with currently setup it is not. I am having one more model “User” as clear from the code, the User model contains User specific details. What should I try out here to make it work.