Django queryset with isnull=True in get_object_or_404
<p>I have 2 records in the posts table, one of the row in table has <strong>rating as NULL</strong> and the other has <strong>rating as 2</strong>, both have same user_id say 5</p>

<p>I implement this first</p>

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

<pre><code>class Rating(TemplateView):
    template_name = 'base/rating.html'
    def get(self,request,slug,*args,**kwargs):
        user_id = request.user.id
        post = get_object_or_404(Post.objects.filter(user_id=user_id,rating__isnull=True))
        return render(request,self.template_name)
</code></pre>

<p><strong>urls.py</strong></p>

<pre><code>url(r'^post/addRating/(?P&lt;slug&gt;.+?)/$',views.Rating.as_view(),name="post_rating"),
</code></pre>

<blockquote>
  <p>So the actual concept is not to render the view if rating column is
  not null</p>
</blockquote>

<p>So the first record with rating null should return 404 page but it is not and the second record display properly</p>

<p>Can any one help me to fix it?</p>

<p>You need read more carefully the django doc, the above code is incorrect.
To use get_object_or_404, you have to write something like(from django <a href="https://docs.djangoproject.com/en/1.10/topics/http/shortcuts/#get-object-or-404" rel="nofollow">doc</a>)</p>

<pre><code>from django.shortcuts import get_object_or_404
def my_view(request):
    my_object = get_object_or_404(MyModel, pk=1)
</code></pre>

<p>For your propurse, you should write something like:</p>

<pre><code>posts = Post.objects.filter(user_id=user_id,rating__isnull=True)
post = posts and posts[0]
</code></pre>

python django