Suppose my user is logged in and he want to see the user details
I want to create an api end point like /userdetails/
Based on the user who logged in it should return back the details.
This is my serializer
class UserDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = [
"id",
"email",
"first_name",
"last_name",
"is_staff",
"is_active",
"date_joined",
"last_login",
"modified_date",
"creation_date",
]
read_only_fields = [
"email",
"is_staff",
"is_active",
"is_superuser",
"date_joined",
"last_login",
]
Now I want to create a viewset for read and edit
I want something like this
class UserDetailsViewSet(
mixins.UpdateModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet,
):
queryset = User.objects.all()
serializer_class = UserDetailsSerializer
and
router.register(r"userdetail",UserDetailsViewSet )
But the problem with the above is I dont want urls like
/userdetail/<pk>
instead only /userdetail
. Because the <pk>
can be obtained from the request.user
solution
After checking some posts, what I want is something without lookup_field
and later overwrite get_object
. Something similar to RetrieveAPIView without lookup field?
question remaining
how to avoid lookup_field
I tried
class UserDetailsViewSet(
mixins.UpdateModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet,
):
queryset = User.objects.all()
serializer_class = UserDetailsSerializer
lookup_field = ''
but it give error
django.core.exceptions.ImproperlyConfigured: "^userdetail/(?P<>[^/.]+)/$" is not a valid regular expression: missing group name at position 18