Home:ALL Converter>Django ManyToMany field filter

Django ManyToMany field filter

Ask Time:2019-11-25T06:36:42         Author:jeremy_lord

Json Formatter

So I have this system where my Post object has a ManyToMany field and it's called Saves. So like for example on Reddit you can save a post. So I got it working and users can save posts, and it adds them to the ManyToMany field. However, I want to filter out these posts and only show the posts where said user is in the ManyToMany field.

Here is my models.py

class Post(models.Model):
    author = models.ForeignKey(User,related_name='posts',on_delete=models.CASCADE)
    saves = models.ManyToManyField(User,blank=True,related_name='post_saves')

I have the saves field connected to the User model Django provides.

And here is my views.py

class PostSaveRedirect(RedirectView):
    def get_redirect_url(self,*args,**kwargs):
        pk = self.kwargs.get("pk")
        slug = self.kwargs.get("slug")
        obj = get_object_or_404(Post,pk=pk,slug=slug)
        url_ = obj.get_absolute_url()
        user = self.request.user
        if user.is_authenticated:
            if user in obj.saves.all():
                obj.saves.remove(user)
            else:
                obj.saves.add(user)
        return url_

So this is all working fine, it adds the user to the ManyToMany field, but now I want to know how I can filter out posts and only display ones where the user is in the ManyToMany field.

Here is my saved posts view.

class PostSaveListView(ListView):
    model = Post
    template_name = 'mainapp/post_saved.html'
    paginate_by = 10
    queryset = models.Post.objects.all()

    def get(self,request):
        posts = Post.objects.all()
        return render(request, self.template_name)

    def get_queryset(self):
        return Post.objects.filter().order_by('-published_date')

So with Post.objects.all(), how can I change it so it will filter to my needs? This is a similar queryset for a user post list view I have

I have been Googling and reading up the docs and other articles but have not found anything that has been able to show me how to filter a ManyToMany field. Any help would be much appreciated

Author:jeremy_lord,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/59023116/django-manytomany-field-filter
Tech :

edit your model like this:\n\n class PostSaveListView(ListView):\n model = Post\n template_name = 'mainapp/post_saved.html'\n paginate_by = 10\n -\n def get(self,request):\n posts = Post.objects.all()\n return render(request, self.template_name)\n\n def get_queryset(self):\n object_list = Post.objects.filter(saves__in=[self.request.user]).order_by('-published_date').distinct()\n return object_list\n",
2019-11-24T23:21:03
yy