gpt4 book ai didi

python - 类型对象 'User' 没有属性 'objects' (AbstractUser) python

转载 作者:行者123 更新时间:2023-12-05 07:30:22 24 4
gpt4 key购买 nike

应用名称后端

用户对象返回错误:

users = User.objects.filter(fb_userid=fb_user_id)
AttributeError: type object 'User' has no attribute 'objects'

模型.py

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
fb_userid = models.CharField(max_length=256)
avatar = models.ImageField(upload_to='avatars/', blank=True, null=True)

views.py

from backend.models import *

def fb_login(request):
# permission_classes = (permissions.AllowAny,)
if request.method == 'POST':
if 'fb_user_id' not in request.data:
return Response({'error':'missing fb_user_id'}, status=status.HTTP_404_NOT_FOUND)
fb_user_id = str(request.data['fb_user_id'])

users = User.objects.filter(fb_userid=fb_user_id)
if users.count() == 0:
user, created = User.objects.get_or_create(username=fb_user_id, email='fb@fb.net')
if created:
user.save()
else:
user = users[0]
token = Token.objects.get_or_create(user=user)[0]
# Likes
likeIds = []
likes = ImageLike.objects.filter(user_id=user.id)
for like in likes:
likeIds.append(like.image.id)
# Saved Places
savedIds = []
saved = SavedRestaurants.objects.filter(user_id=user.id)
for save in saved:
savedIds.append(save.restaurant.id)
jsonData = { 'token': token.key, 'saved': savedIds, 'likes': likeIds }
return Response(jsonData)

settings.py

AUTH_USER_MODEL = 'backend.User'

enter image description here

用户对象返回错误:

users = User.objects.filter(fb_userid=fb_user_id)AttributeError: 类型对象 'User' 没有属性 'objects'

最佳答案

尝试设置

AUTH_USER_MODEL = 'backend.user'.

关于python - 类型对象 'User' 没有属性 'objects' (AbstractUser) python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52398066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com