3

I have a django project and I am using Django Rest Frameowkr. I setup up a model, serializer, view, and url for a users model. I have the urls file. I want to passing in something like a username when the api url is called. I currently have it setup to have a primary key so when I enter a primary key it works. I want to switch it to username. I also want the serializer query to return the user object iwth the usename I pass in. I am using Djangos standard User object from django.contrib.auth.models Here is the code I have

Urls.py

from django.urls import path
from django.contrib.auth.models import User

from .views import UserListView, UserDetailsView
from .views import ProfileListView, ProfileDetailsView
from .views import RoleListView, RoleDetailsView

urlpatterns = [
    path('user/', UserListView.as_view()),
    path('user/<pk>', UserDetailsView.as_view()),
]

serializer.py file

from rest_framework import serializers

from django.contrib.auth.models import User
from users.models import Profile, Role

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'is_staff', 'last_login')

Views.py file

from rest_framework.generics import ListAPIView, RetrieveAPIView

from django.contrib.auth.models import User

from users.models import Profile, Role
from .serializers import UserSerializer, ProfileSerializer, RoleSerializer

class UserListView(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

class UserDetailsView(RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

1 Answer 1

5

Specify the lookup_field in UserDetailsView and change the urls pattern in urls.py as below

# urls.py
urlpatterns = [
    path('user/', UserListView.as_view()),
    path('user/<username>', UserDetailsView.as_view()),
]


# views.py
class UserDetailsView(RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    lookup_field = 'username'
Sign up to request clarification or add additional context in comments.

4 Comments

it worked but i have another question. lets say that i want to do the same thing but find the username in a user that is a foriegnkey to another table. User is the foriegnkey to another model. I wnt to the take that username from the url and do the lookup inside that foriegnkey. @JPG
I won't recommend nested lookup, because it will take time (in the background, Django joins the relevant tables)
If you want to do, currentfield__username in lookup_field
so if I had this... it shoudl work. class ProfileDetailsView(RetrieveAPIView): queryset = Profile.objects.all() serializer_class = ProfileSerializer lookup_field = 'user__username' or should i do it without the quotes around user__username. there is a table called Profile that has a user foriegnkey

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.