satyajit
satyajit

Reputation: 694

Django: AttributeError: 'str' object has no attribute 'objects'

i am getting error while i extracting the value in my AUTH_USER_MODEL table. any help, would be appreciated.

views.py

AUTH_USER_MODEL = settings.AUTH_USER_MODEL

class AllUser(ListAPIView):
    model = AUTH_USER_MODEL
    serializer_class = UserSerializer
    queryset = AUTH_USER_MODEL.objects.all()

serializers.py

AUTH_USER_MODEL = settings.AUTH_USER_MODEL

class UserSerializer(serializers.ModelSerializer):
   class Meta:
       model = AUTH_USER_MODEL
       fields = [
           "username",
           "email",
       ]

Upvotes: 1

Views: 1760

Answers (1)

dirkgroten
dirkgroten

Reputation: 20682

Use get_user_model() method instead (you can import it from django.contrib.auth import get_user_model):

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

class AllUser(ListAPIView):
    model = User
    serializer_class = UserSerializer
    queryset = User.objects.all()  # or User.objects.filter(is_active=True)

settings.AUTH_USER_MODEL is just a string, which can be used when you define a model, (e.g. ForeignKey accepts a string) but not when you need the actual class.

See this for a detailed explanation.

Upvotes: 5

Related Questions