class UserSerializer(serializers.ModelSerializer):
topics= serializers.PrimaryKeyRelatedField(many=True)
class Meta:
model = User
fields = ('id', 'username', 'topics')topics= serializers.PrimaryKeyRelatedField(many=True)
topics= serializers.PrimaryKeyRelatedField(many=True, source ='topics')
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())
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.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.
--
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.