简体   繁体   English

DJANGO自定义用户模型错误“ str”对象没有属性“ objects”

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

I created a custom User model to replace my Django User model. 我创建了一个自定义用户模型来替换我的Django用户模型。 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 我已经在我的设置文件中设置了-> AUTH_USER_MODEL ='accounts.User',但是当我在其中一种视图中使用时,这里出现错误

users = settings.AUTH_USER_MODEL.objects.exclude(id=request.user.id) 用户=设置.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 任何提示赞赏

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. 如错误所述,该设置值是一个字符串-您知道它是因为(正确地)将其设置为settings.py中的字符串。

To get the actual model, you can use get_user_model from django.contrib.auth . 要获取实际模型,可以使用django.contrib.auth get_user_model Or just import your accounts.User model directly, since you know what it is. 或直接导入您的帐户。直接知道用户模型,因为您知道它是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django 用户 Model AttributeError: 'str' object 没有属性 'objects' - Django User Model AttributeError: 'str' object has no attribute 'objects' Django AttributeError:“ str”对象没有属性“ model” - Django AttributeError: 'str' object has no attribute 'model' Python / DJango属性错误:模型对象没有属性对象 - Python/DJango Attribute error : Model object has not attribute objects Django:“ str”对象没有属性“ user” - Django: 'str' object has no attribute 'user' 使用加密的 SearchField 作为自定义用户 model (Django) 的密钥时出错:AttributeError: 'NoneType' object 没有属性 '_meta' - Error when using encrypted SearchField as key for custom user model (Django): AttributeError: 'NoneType' object has no attribute '_meta' django AbstractUser 模型“str”对象没有属性“_meta” - django AbstractUser model 'str' object has no attribute '_meta' 属性错误:“ str”对象没有属性“ read” python-django - Attribute error: 'str' object has no attribute 'read' python-django 'str' object has no attribute 'get' attribute error in django - 'str' object has no attribute 'get' attribute error in django 自定义Django模型字段中的“对象没有属性” - “object has no attribute” in custom Django model field 属性错误:“str”object 没有属性“str” - Attribute Error: 'str' object has no attribute 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM