0

I want to make an api to get the detail view of a blog from a list of published blog posts. To solve that, I am using get_queryset() filters to solve this, but it is simply giving back all the list, i.e. no filter worked.

I have used the code as shown below:

models.py

class BlogModel (models.Model) :
    heading = models.CharField(max_length=254)
    blog = models.TextField()
    author = models.CharField(max_length=254)

views.py

class BlogRetrieveView(generics.RetrieveUpdateDeleteAPIView):
    serializer_class=BlogListSerializer
    queryset=BlogModel.objects.all()
    lookup_field='blog_id'

    def get_queryset(self,*args, **kwargs):
        return BlogModel.objects.filter(
            blog__id=self.kwargs['blog_id']

serializers.py

class BlogListSerializer(serializers.ModelSerializer):
    class Meta:
        model = BlogModel
        fields = '__all__'

urls.py

 url(r'^blog/(?P<blog_id>\d+)/$',BlogRetrieveView.as_view()),

I am getting the following output:

This shows 1 out of 7 blog post shown in a list. Clearly, the filter wasn't applied.

Edit 1: With the given advices, my code on localhost worked, but the production website is still stuck on a situation mentioned in the problem above. What can be the reason behind it?

RJ_SU
  • 31
  • 9

2 Answers2

0

I think you should delete lookup_field and get_queryset() of BlogRetrieveView and change urls to url(r'^blog/(?P<pk>\d+)/$',BlogRetrieveView.as_view())

marke
  • 1,024
  • 7
  • 20
  • The whole method is working in django's latest version. But my website is working on django 1.10 and python 2.7 . It must be having some other ways to do so – RJ_SU Apr 15 '19 at 07:08
  • My bad, its django 1.11 itself – RJ_SU Apr 15 '19 at 10:47
0

If you're using the RetrieveUpdateDeleteAPIView class, you probably don't want to use lookup_field='blog_id' and instead meant to use lookup_url_kwarg='blog_id'. You should also remove your get_queryset() method as the view will handle that for you.

When you change this, your view will retrieve the correct value from the URL r'^blog/(?P<blog_id>\d+)/$' using the lookup_url_kwarg and it will attempt the lookup using the id field on the BlogModel.

Also, if your request to /api/blog/9/ was returning a list then you url configuration must be incorrect because the RetrieveUpdateDeleteAPIView class does not return that JSON, it will only return a dictionary-like object of the data from your model. Is your blog-list url configured as r'^blog/$'? (a missing $ at the end may be the cause)

A. J. Parr
  • 7,731
  • 2
  • 31
  • 46
  • 1
    The knowledge of look_up_kwarg was crucial. Also, as u predicted, the $ in r'^blog/$' was missing. Though the code is working in localhost, its not working in the production website, what can be the reason for this? – RJ_SU Apr 15 '19 at 17:20
  • @RJ_SU do you have any more information about the error in production? Is it the same error? – A. J. Parr Apr 16 '19 at 03:43
  • 1
    I figured it out finally! It all suggested that the server was not restarting with the code changes I made on the git repository. Actually, the restart command I was using was not correct or working properly. The exact command did the job. – RJ_SU Apr 17 '19 at 17:12