类型object'User'没有属性'objects'(AbstractUser)python

时间:2018-09-19 04:29:42

标签: python django python-3.x django-models django-rest-framework

应用名称后端

用户对象返回错误:

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

models.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”

1 个答案:

答案 0 :(得分:0)

尝试设置

AUTH_USER_MODEL = 'backend.user'.