1

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",
       ]
5
  • Use get_user_model() method instead (you can import it from django.contrib.auth import get_user_model). 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. Commented Feb 4, 2020 at 12:53
  • post the error traceback Commented Feb 4, 2020 at 12:55
  • i tried it first, but it gave me all users even some users aren't active in my database. i want users who are active users. have any suggestion, how to do it @dirkgroten Commented Feb 4, 2020 at 12:56
  • queryset = AUTH_USER_MODEL.objects.all() AttributeError: 'str' object has no attribute 'objects' @Mirza715 Commented Feb 4, 2020 at 12:57
  • @SatyajitBarik that has nothing to do with get_user_model(). That's just because your queryset uses all(), change that to filter the users you want. Commented Feb 4, 2020 at 12:59

1 Answer 1

5

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.

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.