Search code examples
pythondjangoauthenticationtastypie

Django 'long' object has no attribute 'member'


I'm trying to handle an exception that's being thrown when users try to login through my Tastypie API, but I'm not sure I have the syntax correct.

def login(self, request, **kwargs):
    self.method_check(request, allowed = ['post'])

    data = self.deserialize(request, request.body, format = request.META.get('CONTENT_TYPE', 'application/json'))

    username = data.get('email', '')
    password = data.get('password', '')
    print username
    #print password

    user = authenticate(username = username, password = password)
    #member = user.member
    #print user 

    if user:
        if user.is_active:
            login(request, user)
            user = user.id

            try:
                print 'user has member'
                member = user.member.id
                return self.create_response(request, {'success' : True, 'member' : member})
            except ObjectDoesNotExist:
                print 'user does not have member'
                return self.create_response(request, {'success' : True, 'member' : False})




        else:
            return self.create_response(request, {'success' : False, 'reason' : 'disabled'}, HttpForbidden)
    else:
        return self.create_response(request, {'success' : False, 'reason' : 'incorrect'}, HttpUnauthorized)

The error is given at member = user.member.id in the try.

member = user.member.id\n\nAttributeError: 'long' object has no attribute 'member'

Any help is greatly appreciated thanks.


Solution

  • The following line overwrites user with id attribute value; user now reference long object; causes AttributeError.

        user = user.id
    

    Remove the line.