I am trying to limit a set of list objects made a available to those created by specific users in Django Rest Framework. I have been following the DRF documentation to filter against current user, the main difference I have is that User is defined in a seperate model to the the objects I am working with. I'm trying to use a dunder to fix this but the data isn't filtering correctly.
All list objects are available to any user rather than only some objects being available based on the creator who is logged in.
I'm wondering if anyone could propose a way forward?
Here are the models:
class UserList(models.Model):
list_name = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE)
class UserVenue(models.Model):
venue = models.ForeignKey(mapCafes, on_delete=models.PROTECT)
list = models.ForeignKey(UserList, on_delete=models.PROTECT)
And here are the views.py
#this is where a user can assign cafe objects to their list object (but currently can see all lists)
class UserVenueViewSet(viewsets.ModelViewSet):
serializer_class = UserVenueSerializer
queryset = UserVenue.objects.all()
def get_queryset(self):
user = self.request.user
return UserVenue.objects.get_queryset().filter(list__user=user)
#this shows all lists for a user
class UserList(viewsets.ModelViewSet):
serializer_class = UserListSerializer
queryset = UserList.objects.all()
def get_queryset(self):
return super().get_queryset().filter(user=self.request.user)