Django where in queryset with comma separated slug in url
<p>I have a Post Model</p>

<p><strong>models.py</strong></p>

<pre><code>class Category(models.Model):
    title = models.CharField(max_length=100,null=True,blank=False)
    slug = models.SlugField(max_length=150,null=True,blank=False)

class Post(models.Model):
    title = models.CharField(max_length=256,null=True,blank=False)
    categories = models.ManyToManyField(Category)
</code></pre>

<p>So in view I need to query all the posts with specific category for example
This is the url <a href="http://example.com/?category=slug1,slug2" rel="nofollow">http://example.com/?category=slug1,slug2</a> or <a href="http://example.com/?category=1,2" rel="nofollow">http://example.com/?category=1,2</a> - PK or slug will be used</p>

<p><strong>views.py</strong></p>

<pre><code>class PostList(TemplateView):
    ...
    def get(self,request,*args,**kwargs):
        categories = request.GET.get('category')
        post_list = Post.objects().filter(categories__in=categories)
    ...
</code></pre>

<p>The above view throws error as <code>invalid literal for int() with base 10: ','</code> because of passing comma separated strings, but how can I achieve this?</p>

<p>Comma separated you're going to have to do this by hand:</p>

<p><code>categories = request.GET.get('category').split(',')</code></p>

<p>The more Django way to do this it so change your query string to be</p>

<p><code>?category=slug1&amp;category=slug2</code>.  </p>

<p>Then you could use</p>

<p><code>categories = request.GET.getlist('category')</code></p>

python django