[Django]-Django Rest Framework โ€“ Field username with ".", "-", "_" character

4๐Ÿ‘

โœ…

If you look at the error youโ€™ll notice that it tried to match against some url, but failed. This is because the default regex ([^./]) excludes . and / characters

You can add a lookup_value_regex to your ViewSet so that the URL knows what format your primary key should be in:

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    lookup_value_regex = '[\w.@+-]+'
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    filter_fields = ('username', 'is_player', 'first_name', 'last_name', 'team' , 'email', )

Leave a comment