1👍
✅
use snippets not snippet, try with
class UserSerializer(serializers.ModelSerializer):
snippets = serializers.PrimaryKeyRelatedField(many=True)
class Meta:
model = User
fields = ('id', 'username','snippets')
2👍
Please take a look at your snippets/models.py
owner = models.ForeignKey('auth.User', related_name='snippets')
The thing you missed is related_name argument.
class UserSerializer(serializers.ModelSerializer):
snippets = serializers.PrimaryKeyRelatedField(many=True)
class Meta:
model = User
fields = ('id', 'username', 'snippets')
related_name argument on related model ForeignKey should match both “field name” of you serializer also be inside fields tuple.
👤rivf
- [Django]-"error": "invalid_client" django-oauth-toolkit
- [Django]-Django TastyPie Geo Distance Lookups
0👍
Here is another way of doing this
class snippetSerializer(serializers.PrimaryKeyRelatedField, serializers.ModelSerialier):
class Meta:
model = Snippet
fields = '__all__'
class UserSerializer(serializers.ModelSerializer):
snippets = snippetSeriazlier(many=True, queryset=Snippet.objects.all())
class Meta:
model = User
fields = ('id', 'username','snippets')
- [Django]-Django formfield_overrides conflicts with max_length in TextField
- [Django]-What should be the directory structure for a REST API in Django?
- [Django]-Django manage.py spawning several fcgi processes
- [Django]-Django template How to Convert numbers to Words
Source:stackexchange.com