1

After finishing tutorial 1-3 smoothly, I followed the tutorial (http://django-rest-framework.org/tutorial/4-authentication-and-permissions.html) and finished sections up to "Adding endpoints for our User models".

(In other words, adding "url(r'^users/$', views.UserList.as_view()), url(r'^users/(?P[0-9]+)/$', views.UserInstance.as_view())," was done.)

Then, I ran the server with the command of "python manage.py runserver" and point my browser to http://127.0.0.1:8000/users/ and got the following error message:

(message starts)

NameError at /users/

name 'User' is not defined

Request Method: GET

Request URL: http://127.0.0.1:8000/users/

Django Version: 1.4.3

Exception Type: NameError

Exception Value: name 'User' is not defined

Exception Location: /home/user/tutorial/snippets/serializers.py in Meta, line 14

(message ends)

Did I miss something?

The code in my serializer.py was:

from django.forms import widgets
from rest_framework import serializers
from snippets import models

class SnippetSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Snippet
        fields = ('id', 'title', 'code', 'linenos', 'language', 'style')

class UserSerializer(serializers.ModelSerializer):
    snippets = serializers.ManyPrimaryKeyRelatedField()

    class Meta:
        model = User
        fields = ('id', 'username', 'snippets')

1 Answer 1

2

You'll need to import Django's user class.

from django.contrib.auth.models import User

That import line isn't currently mentioned. I guess it probably should be.

Sign up to request clarification or add additional context in comments.

Comments

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.