1

My main urls.py is located here ahlami -> ahlami -> urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/accounts/', include('accounts.api.urls')),
]

My accounts app urls.py is located here ahlami -> accounts -> api -> urls.py

urlpatterns = [
    path('users/<int:pk>/', views.UserView.as_view(), name='user-detail')
]

One of my accounts views.py returns

token = Token.objects.create(......)
return Response(data=AnonymousUserTokenResponseSerializer(instance=token).data)

My token model has three fields only. For simplicity, I listed one field below

class Token(rest_framework.authtoken.models.Token):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE

AnonymousUserTokenResponseSerializer is linked to the Token model and returns three json attributes

class AnonymousUserTokenResponseSerializer(serializers.ModelSerializer):
    user_id = serializers.ReadOnlyField(source='user.id')
    user_url = reverse(viewname='user-detail')
    class Meta:
        model = Token
        fields = ('key',
                  'user_id',
                  'user_url')

AnonymousUserTokenResponseSerializer fails because it can't identify reverse()

user_url = reverse(viewname='user-detail')

python manage.py runserver throws the error below because of the line above

django.core.exceptions.ImproperlyConfigured: The included URLconf 'ahlami.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

My settings is located here ahlami -> ahlami -> settings -> base.py and base.py has this

ROOT_URLCONF = 'ahlami.urls'

I expect to get an output that looks like but couldn't because of the error above.

{
    "key": "891e388399f2fcae016fe6887107034239041478",
    "user_id": 29,
    "user_url": http://localhost/api/accounts/users/29 
}

How can I resolve this error and make reverse() work?

django.core.exceptions.ImproperlyConfigured: The included URLconf 'ahlami.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

3
  • It doesn't make any sense that you have put user_url in the serializer since it is not a serializer field Commented Aug 13, 2020 at 18:26
  • What do you mean? Do you mean user_url is not part Token? user_id is not part of the serializer either. I declared user_id and it worked. Commented Aug 13, 2020 at 18:30
  • user_id is a DRF Serializer field, but user_url is not Commented Aug 13, 2020 at 18:32

1 Answer 1

1

Use serializers.HyperlinkedIdentityField

class AnonymousUserTokenResponseSerializer(serializers.ModelSerializer):
    user_id = serializers.ReadOnlyField(source='user.id')
    user_url = serializers.HyperlinkedIdentityField(
        view_name='user-detail',
        source='user.id',
        lookup_field='pk'
    )

    class Meta:
        model = Token
        fields = ('key', 'user_id', 'user_url')
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! It worked. Thanks Arakkal... I had to do few changes. I had to pass context in my serializer's method return Response(data=AnonymousUserTokenResponseSerializer(instance=token, context={'request': request}).data) and I had to change to serializers.HyperlinkedIdentityField to serializers.HyperlinkedRelatedField user_url = serializers.HyperlinkedRelatedField( view_name='user-detail', source='user.id', lookup_field='pk', read_only='True' )
Just trying to understand why I had to pass return context={'request': request}. If you have any thoughts, feel free to share it here. Anyway, I'll google it.
coz, DRF uses the request object internally to build the URL

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.