[英]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.