0

I have search every where but no one stated this error before.

The obj will return a unicode object but it will return the following error

Exception Type: AttributeError  
Exception Value:'unicode' object has no attribute 'pk'

It works if I hard code the result from the response.
CustomerAccount.py

from django.contrib.auth.models import User  

check login

 return user

api.py

result = CustomerAccount.login(username, password)  
return HttpResponse(json.dumps(result), content_type="application/json")

views.py

import urllib2  
import json

res = urllib2.urlopen("http://127.0.0.1:8000/api/login?username=admin&password=admin").read()  
obj = json.loads(res)
print obj[0].pk

Result of print obj:

[{"pk": 1, "model": "auth.user", "fields": {"username": "admin", "first_name": "Admin", "last_name": "admin", "is_active": true, "is_superuser": true, "is_staff": true, "last_login": "2013-05-29T08:08:43.859Z", "groups": [], "user_permissions": [], "password": "pbkdf2_sha256$10000$1HdCOPgsoXvx$8jjOpTFVcVAtUshpjJDPEGs/TRq7jeJ2T/2i55FIPeM=", "email": "[email protected]", "date_joined": "2013-05-15T07:59:30Z"}}]

1
  • You're looking up a dictionary, shouldn't it be print obj[0]['pk']? Commented Jun 5, 2013 at 6:52

2 Answers 2

3

You got your types wrong:

  • obj is a list
  • obj[0] is a dict
  • a dict has no pk attribute, however you can retrieve the value for key "pk" with: obj[0]['pk']
Sign up to request clarification or add additional context in comments.

7 Comments

Tried print obj[0]['pk'] and I have this error Exception Type: TypeError Exception Value: string indices must be integers
tried obj[0].get('pk') this is the following error Exception Type: AttributeError Exception Value: 'unicode' object has no attribute 'get'
It seems like obj is a list of string when i do print obj[0] it will give me the first char of the string.
this is not a proper (but valid if javascript) json object for python. btw. You're mixing in javascript datatypes, true unquoted. Python will spazz on this.
Tried the changing the true to True but no different. It is still unicode object.
|
2

You say you get

Exception Value:'unicode' object has no attribute 'pk'

when retrieving obj[0].pk . If it was a data type error, you would get 'dict' object has no attribute 'pk' instead.

So the problem is that your obj[0] is not a dict as you expect, or a list, but it is a unicode string.

As per comments, this is what happens:

  1. You generate the JSON object server side.
  2. JSON-encode it. You now have a unicode string.
  3. JSON-encode it again. Now you have a unicode string as a JSON object.

  4. Retrieve it from the URL.

  5. Decode it once. You now have the unicode string which encodes the object.
  6. Extract pk from the unicode string... and you can't do that.

A quick fix would be to decode the object twice. The real fix is detect where the double encoding takes place and prevent that from happening.

The telltale that should have told me what had happened was this:

[{"pk": 1, "model": ...

If that had been a Python object (instead of a JSON encoding), it would have been:

[{u'pk': 1, u'model': ...

6 Comments

Hardcoding works but if i retrieve it from the url it does not work.
Yes, I saw it on re-reading your question and updated the answer.
This is the output of the URL return "[{\"pk\": 1, \"model\": \"auth.user\", \"fields\": {\"username\": \"admin\", \"first_name\": \"Admin\", \"last_name\": \"admin\", \"is_active\": true, \"is_superuser\": true, \"is_staff\": true, \"last_login\": \"2013-05-29T08:08:43.859Z\", \"groups\": [], \"user_permissions\": [], \"password\": \"pbkdf2_sha256$10000$1HdCOPgsoXvx$8jjOpTFVcVAtUshpjJDPEGs/TRq7jeJ2T/2i55FIPeM=\", \"email\": \"[email protected]\", \"date_joined\": \"2013-05-15T07:59:30Z\"}}]"
It is a User object and was JSON.dumps into a http response.
How i dump it: return HttpResponse(json.dumps(result), content_type="application/json")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.