Reputation: 109
How can I retrieve a user by username rather than the primary key while using the rest frameworks ModelViewSet
?
Here is my current view:
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
Upvotes: 1
Views: 105
Reputation: 7717
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
lookup_field = 'username'
If you want to use object lookups other than pk, set 'lookup_field'
.
Upvotes: 2