Created
March 29, 2012 20:43
-
-
Save cridenour/2243612 to your computer and use it in GitHub Desktop.
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
from django.db import models | |
class Moment(models.Model): | |
title = models.CharFiend(max_length=128) | |
image = models.ImageField(upload_to='moments') | |
description = models.TextField() | |
uploaded = models.DateTimeField(auto_now_add=True, blank=True) | |
def __unicode__(self): | |
return self.title |
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
from django.views.generic import View | |
import json | |
from django.shortcuts import HttpResponse | |
from myapp.models import Moment | |
class MomentFeed(View): | |
def get(self, request, *args, **kwargs): | |
moments = Moment.objects.all().order_by('-uploaded') # Load all moments in reverse order | |
# Now convert to nice dicts for use in JSON | |
json_moments = [] | |
for m in moments: | |
json_moments.append({ | |
'id': m.id, | |
'title': m.title, | |
'image': m.description, | |
}) | |
return HttpResponse(status=200, content_type='application/json', content=json.dumps(json_moments)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Totally untested, but point your url in urls.py to the MomentFeed (have to import it fist) to get an idea.