Last active
August 29, 2015 14:15
-
-
Save snobear/066a263d0b0aa035bab9 to your computer and use it in GitHub Desktop.
getstream return enriched as json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this throws: <stream_django.enrich.EnrichedActivity object at 0x7f8ffc86e250> is not JSON serializable | |
from django.http import HttpResponse, HttpResponseServerError | |
from stream_django.enrich import Enrich | |
from stream_django.feed_manager import feed_manager | |
import json | |
def enrich_feed(request): | |
context = {} | |
try: | |
# raw feed data | |
activities = json.loads(request.body) | |
# convert to django objects | |
enricher = Enrich(fields=['actor', 'object', 'target', 'station']) | |
context['activities'] = enricher.enrich_activities(activities) | |
#response = render_to_response('mystream/feed.html', context) | |
return HttpResponse(json.dumps(context), content_type='application/json') | |
except Exception, e: | |
response = HttpResponseServerError(e) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm getting
is not JSON serializable
with that. Heres a test with a hard-coded example (although you can't run it from your side without my models.py I guess), but to illustrate:It seems to be choking when trying to serialize my custom models? Or more specifically, the queryset for each custom model? e.g.
u'target': <Playlist: Good Picks>
is probably one its unable to serialize.I see some info on SO about it, but I'm wondering how to incorporate the solutions. I think I need to flatten out Playlist value, for example? Is it possible to have a method in each of my models that returns a json-serializable? Or maybe I could loop through the acts_enriched and flatten/fix them before running json.dumps. Thoughts?
http://stackoverflow.com/questions/16336271/is-not-json-serializable
http://stackoverflow.com/questions/10764909/django-serialize-datetime-to-json-in-queryset-dict