3

In my codes, I have a model Tweet, and in tweet_list_view, I want to show the list of tweets as API view.

@api_view(['GET'])
def tweet_list_view(request, *args, **kwargs):
    qs = Tweet.objects.all().order_by('-date_posted')
    serializer = TweetSerializer(data=qs, many=True)
    return Response(serializer.data)

This is what I got as a result.

AssertionError at /tweets/
When a serializer is passed a `data` keyword argument you must call `.is_valid()` before attempting to access the serialized `.data` representation.
You should either call `.is_valid()` first, or access `.initial_data` instead.

So I called the .is_valid method like following:

@api_view(['GET'])
def tweet_list_view(request, *args, **kwargs):
    qs = Tweet.objects.all().order_by('-date_posted')
    serializer = TweetSerializer(data=qs, many=True)
    if serializer.is_valid():
        return Response(serializer.data, status=201)
    return Response({}, status=400)

Then I get:

TemplateDoesNotExist at /tweets/
rest_framework/api.html

At serializers.py class TweetSerializer(serializers.ModelSerializer): class Meta: model = Tweet fields = ['content', 'date_posted', 'likes']

models.py

class Tweet(models.Model):
    content = models.TextField(blank=True, null=True)
    image = models.FileField(upload_to='images/', blank=True, null=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
    date_posted = models.DateTimeField(default=timezone.now)
    likes = models.IntegerField(default=0)

    def __str__(self):
        return self.content

    class Meta:
        ordering = ['-date_posted']

It's looking for a template, but it's supposed to use the default Django template. Is there any way to fix this problem?

2
  • 1
    did add rest_framework to INSTALLED_APPS ? Commented Jul 8, 2020 at 3:32
  • 2
    Also, you don't need to use data argument to serialize model objects Commented Jul 8, 2020 at 3:34

1 Answer 1

2

Update:

forgot the @ in front of the api_view. Added it. Also added the renderer_class (jsonrenderer) to make sure avoiding the error.


You need to use the data attribute of the serializer only when you have a post, put or patch view. In your case just try it without the data attribute and it should be fine

from rest_framework.renderers import JSONRenderer

@api_view(['GET'])
@renderer_classes([JSONRenderer]) 
def tweet_list_view(request, *args, **kwargs): 
    qs = Tweet.objects.all().order_by('-date_posted') 
    serializer = TweetSerializer(qs, many=True)
    return Response(serializer.data)

Here you can see it in the tutorial example of the official django rest framework docs

Sign up to request clarification or add additional context in comments.

6 Comments

I used your code, but I got AssertionError at /tweets/.accepted_renderer not set on Response
can you please add your model and your serializer?
I don't think its related to your model or serializer, instead you should look at this
can you try to add the renderer_class for your function-based view. Here is a reference where this problem is solved by adding the renderer_class: https://stackoverflow.com/a/55417280/13347371
well that was at the same time :D I dont use function-based views very often, thats why I didnt notice that at first.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.