I don’t want to allow any user to update another user’s object. I want to allow only update user’s own content only. here is my code:
models.py
class Blog(models.Model):
author = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True)
blog_title = models.CharField(max_length=200, unique=True)
views.py
class BlogViewSet(viewsets.ModelViewSet):
queryset = Blog.objects.all()
serializer_class = BlogSerializer
pagination_class = BlogPagination
lookup_field = 'blog_slug'
def update(self, request, slug=None):
pass
How can I overwrite the update method? I tried somethings like this but didn’t work:
def update(self, instance, validated_data):
request = self.context.get("request")
if instance.author.id == request.user.id:
instance.save()