I am trying to encode an object into json using json.dumps()
in Django, however when I pass in a python object, it raises this error.
TypeError: <OrgInvite: OrgInvite object> is not JSON serializable
I was under the assumption that even though JSON can only encode certain data types, one of those data types were objects. I read another question on Stack Overflow that a good way to get around this is by creating a dictionary out of the object using .__dict__
I tried this and it is saying that one of the keys in my new dictionary, _state is not serializable. I am not sure where this _state key came from, and was wondering is there a way to convert my object into a dictionary without that extra field, so I can encode it into JSON ?
model:
class OrgInvite(models.Model):
token = models.CharField(max_length=16, unique=True, null=False)
account_id = models.ForeignKey(Account, on_delete=models.CASCADE, null=False)
org_id = models.ForeignKey(Org, on_delete=models.CASCADE, null=False)
used = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
name = models.CharField(max_length=70)
email = models.CharField(max_length=255)
view:
def get_invite(token):
if not token:
raise Exception("Invitation token is not specified")
invitation = OrgInvite.objects.get(token=token)
if not invitation:
raise Exception("Invitation token is invalid.")
return invitation
def invite_accept_redirect(token):
# """ -Redirects to the accept invite frontend view with pre-fetched data. """
try:
invite = get_invite(token)
if not invite:
raise Exception("Invitation token is invalid")
if invite.used:
invite = {'used': True}
except:
invite = {'invalid': True}
raise Exception("Resource not found.")
base = "home/accept"
url = '{}/{}?data={}'.format(base, token, urllib.quote_plus(json.dumps(invite.__dict__)))
return redirect(url)
console:
>>> oi = OrgInvite.objects.get(token=100)
>>> oi
<OrgInvite: OrgInvite object>
>>> oix = oi.__dict__
>>> oix
{'used': False, 'name': u'', '_state': <django.db.models.base.ModelState object at 0x10377a610>, 'email': u'', 'token': u'100', 'org_id_id': 101, 'account_id_id': 301, 'is_admin': False, 'id': 1}
>>> json.dumps(oix)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 244, in dumps
return _default_encoder.encode(obj)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <django.db.models.base.ModelState object at 0x10377a610> is not JSON serializable
._asdict()
and returns the thing as dictionary. It may be valuable to use that same interface for your code. – Wayne Werner Apr 11 '16 at 18:02namedtuple
– wim Apr 11 '16 at 18:10._asdict()
method. I mentionnamedtuple
as a source in the standard library that already does that. Perhaps it's not quite appropriate for the OP's case, but may accomplish what they need (or others who have a similar problem). – Wayne Werner Apr 11 '16 at 18:19