11

I have made a custom user model,by referring the tutorial , this is how I serialize the new user model:

Serializers.py

from django.conf import settings
User = settings.AUTH_USER_MODEL

class UserSerializer(serializers.ModelSerializer):
    post = serializers.PrimaryKeyRelatedField(many=True, queryset=Listing.objects.all())
    class Meta(object):
        model = User
        fields = ('username', 'email','post')

Views.py

from django.conf import settings
User = settings.AUTH_USER_MODEL
class UserList(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

But when I tried to use this serializer,I get

'str' object has no attribute '_meta'

What did I do wrong?

Antal Spector-Zabusky
  • 36,191
  • 7
  • 77
  • 140
dev-jim
  • 2,404
  • 6
  • 35
  • 61

1 Answers1

20

Instead of

User = settings.AUTH_USER_MODEL

use

from django.contrib.auth import get_user_model
User = get_user_model()

Remember that settings.AUTH_USER_MODEL is just a string that indicates which user model you will use not the model itself. If you want to get the model, use get_user_model

levi
  • 22,001
  • 7
  • 73
  • 74
  • 2
    Yes, you are right. But for django 1.8 it should be `from django.contrib.auth import get_user_model` – dev-jim Sep 17 '15 at 19:56
  • 2
    @levi Thanks for the answer, just a small correction is needed. You have written "from from django.contrib.auth import get_user_model" Please update it to single from. – Yash Rastogi Jun 06 '18 at 04:36