Quantcast

Django rest framework cannot deal with multple objects in model viewset

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Django rest framework cannot deal with multple objects in model viewset

Robin Lery
I have a very simple model and its related serializer and views:

class Page(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=255)
    pub_date = models.DateTimeField(default=timezone.now)

class PageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Page
        fields = ('user', 'title', 'pub_date')

class PageViewSet(viewsets.ModelViewSet):
    queryset = Page.objects.all()
    serializer_class = PageSerializer
Now I can post like this:

{
    "user": 1,
    "title": "Lorem ipsum"
}
This works fine. But I would like to post multiple objects like this:

[
    {
        "user": 1,
        "title": "Lorem ipsum one"
    },
    {
        "user": 1,
        "title": "Lorem ipsum two"
    }
]
But this gives me an error:

"non_field_errors": [

"Invalid data. Expected a dictionary, but got list."
]
So to accept multple objects I modified the view as per the doc:

class PageViewSet(viewsets.ModelViewSet):
    queryset = Page.objects.all()
    serializer_class = PageSerializer(queryset, many=True)

But I am getting an error:

TypeError at /api/blog/pages/

'ListSerializer' object is not callable
What am I missing here?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B4-nGqCNu-03O7%3DvDO5G%3DSOA1jW7ef3%3Dfm%3DV1SNbX%2BX9sAtZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Django rest framework cannot deal with multple objects in model viewset

Xavier Ordoquy

Le 21 avr. 2017 à 04:17, Robin Lery <[hidden email]> a écrit :

I have a very simple model and its related serializer and views:

class Page(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=255)
    pub_date = models.DateTimeField(default=timezone.now)

class PageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Page
        fields = ('user', 'title', 'pub_date')

class PageViewSet(viewsets.ModelViewSet):
    queryset = Page.objects.all()
    serializer_class = PageSerializer
Now I can post like this:

{
    "user": 1,
    "title": "Lorem ipsum"
}
This works fine. But I would like to post multiple objects like this:

[
    {
        "user": 1,
        "title": "Lorem ipsum one"
    },
    {
        "user": 1,
        "title": "Lorem ipsum two"
    }
]
But this gives me an error:

"non_field_errors": [

"Invalid data. Expected a dictionary, but got list."
]
So to accept multple objects I modified the view as per the doc:

class PageViewSet(viewsets.ModelViewSet):
    queryset = Page.objects.all()
    serializer_class = PageSerializer(queryset, many=True)

But I am getting an error:

TypeError at /api/blog/pages/

'ListSerializer' object is not callable
What am I missing here?

it’s serializer_class, not serializer_instance. You can’t instantiate the serializer there.
In order to deal with multiples items, you better override the `list` method to handle multiple objects creation and have an explicit serializer instantiation.

Regards,
Xavier Ordoquy,
Linovia.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/645FD5F7-649C-4454-9450-93AD87B6FA5C%40linovia.com.
For more options, visit https://groups.google.com/d/optout.
Loading...