Serializer related fields must include a 'queryset' argument or set 'read_only=True

8,759 views
Skip to first unread message

Willy

unread,
Sep 17, 2013, 5:43:59 AM9/17/13
to django-res...@googlegroups.com
Hi,

I have a problem with authetication. I use the lastest version of djangorestframework 2.3.8

When i follow this tutorial online, i have an error in my project, and i can't see the users list at my url : 127.0.0.1:8000/member/api/

The browser send me this error : Serializer related fields must include a 'queryset' argument or set 'read_only=True

My UserSerializer look like that :
class UserSerializer(serializers.ModelSerializer):
    topics= serializers.PrimaryKeyRelatedField(many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'topics')
In my TopicSerializer i have add :

author = serializers.Field (source='author.username').

Please, somebody can help me ?

Carlton Gibson

unread,
Sep 17, 2013, 5:53:27 AM9/17/13
to django-res...@googlegroups.com
Hi Willy, 

On 17 Sep 2013, at 11:43, Willy <willy....@gmail.com> wrote:

topics= serializers.PrimaryKeyRelatedField(many=True)
It looks like you need to include the `source="topics"` argument here. — Hopefully that should fix it.

Willy

unread,
Sep 17, 2013, 5:58:49 AM9/17/13
to django-res...@googlegroups.com
So, i need to to this ?

topics= serializers.PrimaryKeyRelatedField(many=True, source ='topics')
Or
author = serializers.Field (source='topic'). 
?

Tom Christie

unread,
Sep 17, 2013, 6:16:47 AM9/17/13
to django-res...@googlegroups.com
Hiya,

Looks like there's a tweak need to the tutorial there.
The exception is giving the right information here... "Serializer related fields must include a 'queryset' argument or set 'read_only=True`"
So in this case, change the serializer field like so:

    topics = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

The reason you're seeing this message is that relationships need an explicit queryset from which to select the valid model instances that may be used when updating the field.
If you wanted a read-write field, you'd instead write something like this...

    topics = serializers.PrimaryKeyRelatedField(many=True, queryset=Topic.objects.all())

Hope that helps!

  Tom :)

Willy

unread,
Sep 17, 2013, 7:17:05 AM9/17/13
to django-res...@googlegroups.com
Thank you, when a put 'read_only=True` it's work better.

But a have another error now. The browser tell me that :'User' object has no attribute 'topics'. However, I did like in the doc. I import the class User of  Django, and this I know that this User model haven't topics argument, but how do you doing for it's work with snippets ?

Thank you for you answer

Carlton Gibson

unread,
Sep 17, 2013, 7:20:00 AM9/17/13
to django-res...@googlegroups.com
Hi Tom, 

Thanks for stepping in with the right answer. :-)

On 17 Sep 2013, at 12:16, Tom Christie <christ...@gmail.com> wrote:
The exception is giving the right information here... "Serializer related fields must include a 'queryset' argument or set 'read_only=True`"
So in this case, change the serializer field like so:

    topics = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

The reason you're seeing this message is that relationships need an explicit queryset from which to select the valid model instances that may be used when updating the field.
If you wanted a read-write field, you'd instead write something like this...

    topics = serializers.PrimaryKeyRelatedField(many=True, queryset=Topic.objects.all())

I'm confused by the observed behaviour given the docs though: 

queryset - By default ModelSerializer classes will use the default queryset for the relationship. Serializer classes must either set a queryset explicitly, or set read_only=True.

Willy has subclassed ModelSerializer, so shouldn't it be using "the default queryset for the relationship"? — I've banged on this behaviour a lot (using ModelSerializer subclasses) and have never needed to specify the queryset here. 


Carlton Gibson

unread,
Sep 17, 2013, 7:22:32 AM9/17/13
to django-res...@googlegroups.com
Hi Willy, 

On 17 Sep 2013, at 13:17, Willy <willy....@gmail.com> wrote:

Thank you, when a put 'read_only=True` it's work better.

But a have another error now. The browser tell me that :'User' object has no attribute 'topics'. However, I did like in the doc. I import the class User of  Django, and this I know that this User model haven't topics argument, but how do you doing for it's work with snippets ?

This sounds as if you haven't set the `related_name` attribute when setting the ForeignKey to the User model in you Topic model. If you don't set this explicity — related_name='topics' — it will default to 'topic_set` IIFC. 

Something like this in models.py:

class Topic(db.Model):
user = db.ForeignKey(User, related_name="topics"
...


HTH

Regards,

Carlton

Carlton Gibson

unread,
Sep 17, 2013, 7:27:09 AM9/17/13
to django-res...@googlegroups.com

On 17 Sep 2013, at 13:22, Carlton Gibson <carlton...@gmail.com> wrote:

This sounds as if you haven't set the `related_name` attribute when setting the ForeignKey to the User model in you Topic model.

... but if there is no relationship named `topics` then there won't be a default queryset for it ... 

[Exit Stage Left. Whistling.]

Willy

unread,
Sep 17, 2013, 7:51:59 AM9/17/13
to django-res...@googlegroups.com
Thank you so much. The problem come from there.

I have put this, and now it's work fine :
class Topic(db.Model):
user = db.ForeignKey(User, related_name="topics"
...

Thanks

Carlton Gibson

unread,
Sep 17, 2013, 7:59:44 AM9/17/13
to django-res...@googlegroups.com
You're welcome. :-) 

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages