2

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

1 Answer 1

2

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.

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

1 Comment

your answer is spot on @Daniel Roseman thanks for help. totally missed this trick.

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.