Tuesday, May 4, 2010

Working out some custom API stuff

Here's the deal. I'm working on a custom API for moving information
about. I am stuck at a point in creating my view.
It might be best just to get into the details.

Here is the model:


class entry(models.Model):
question = models.CharField('Question', max_length=200,
blank=False)
answer = models.TextField('Answer', blank=False)
answerer = models.CharField("Expert's Name", max_length=100,
blank=False)
credentials = models.CharField ("Expert's Credentials",
max_length=100, blank=False)
published = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
site = models.ForeignKey(Site)
section = TagField()

Once a site is picked the feed is narrowed down by id (whether single
id, list or range), then by section (single or multiple) and finally
some pagination and count params (I'm not worried about that right
now). Currently, I am stuck on the section portion. I may be going
about this all wrong. What I would like is for some URL like:

http://mysite.com/qanda/SITENAME/?sect=THESECTIONNAME

to produce a JSON output of Questions and Answers under that site with
that section name. Here is my view:


def QandAAPI(request, site):
quest = entry.objects.filter(site__name__icontains=site)
if 'id' in request.GET:
id = request.GET.get('id','')
id = id.split(',')
else:
id = quest.values_list('id', flat=True)
if 'sect' in request.GET:
sect = request.GET.get('sect','')
else:
sect = ''
quest = quest.filter(id__in=id, section=sect)
object = serializers.serialize("json", quest)
json = "%s" % object
return HttpResponse(json)

Basically, I would like the "else: sect=" to be a WILDCARD so if the
sect portion of the API isn't explicitly stated then it just pulls
everything.

What do you think? Raw SQL? I'm a complete bonehead? All opinions/
advice are welcome.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate