project_kingz
project_kingz

Reputation: 249

DJANGO custom user model error 'str' object has no attribute 'objects'

I created a custom User model to replace my Django User model. I have set in my settings file -> AUTH_USER_MODEL = 'accounts.User' , but when I use in one of my views I get an error here

users = settings.AUTH_USER_MODEL.objects.exclude(id=request.user.id)

class HomeView(TemplateView):
    template_name = 'home/home.html'

    def get(self, request):
        form = HomeForm()
        posts = Post.objects.all().order_by('-date_created')
        users = settings.AUTH_USER_MODEL.objects.exclude(id=request.user.id)

        try:
            friend = Friend.objects.get(current_user=request.user)
            friends = friend.users.all()
        except Friend.DoesNotExist:
            friends = None

        args = {'form': form, 'posts': posts, 'users': users, 'friends': friends}
        return render(request, self.template_name, args)

The error is as follows

    users = settings.AUTH_USER_MODEL.objects.exclude(id=request.user.id)
AttributeError: 'str' object has no attribute 'objects'

Any tips appreciated

Upvotes: 2

Views: 2309

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600051

As the error says, that settings value is a string - you know it is, because you (correctly) set it to a string in your settings.py.

To get the actual model, you can use get_user_model from django.contrib.auth. Or just import your accounts.User model directly, since you know what it is.

Upvotes: 2

Related Questions