GrandGTO
GrandGTO

Reputation: 345

Django Rest Framework return empty JSON

I just start to implement Django Rest Framework by following several tutorials, however I can't fix my issue.

Actually, my API return empty JSON string :

[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]

This my code :

from rest_framework import serializers from wall.models import Articles

serializer.py

class ArticlesSerializer(serializers.Serializer):
    class Meta:
        model = Articles
        fields = ('title',)

views.py

class ArticlesView(generics.ListAPIView):
    queryset = Articles.objects.all()
    serializer_class = ArticlesSerializer

urls.py

url('articles/', ArticlesView.as_view(), name="api-articles"),

models.py

class Articles(models.Model):
    title = models.CharField(max_length=100, null=False, verbose_name="Titre")

I have many articles, so the JSON is returning as many article as I have in my database, but nothing else is displayed. Why ?

Upvotes: 3

Views: 2644

Answers (1)

Mojtaba Kamyabi
Mojtaba Kamyabi

Reputation: 3620

You should inherit your serializer from serializers.ModelSerializer not serializers.Serializer:

class ArticlesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Articles
        fields = ('title',)

Upvotes: 10

Related Questions