On May 5, 4:46 am, Nick <[email protected]> wrote:
> 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.
>

I wouldn't call you a bonehead, but the method you're using to get all
the objects when no id is passed in is... well... special.

The key to doing this sort of filtering is to remember that QuerySets
are lazy, and successive filters can be applied to them without
hitting the database, until the time comes to actually evaluate them.
So what you want to do is to set up the base query at the start of the
method, then further filter it depending on the GET parameters. Then,
if a particular parameter isn't passed in, you simply don't filter on
it. Something like:

def QandAAPI(request, site):
    quest = entry.objects.filter(site__name__icontains=site)
    if 'id' in request.GET:
        ids = request.GET.getlist('id')
        quest = quest.filter(id__in=ids)
    if 'sect' in request.GET:
        sect = request.GET['sect']
        quest = quest.filter(section=sect)
    json = serializers.serialize("json", quest)
    return HttpResponse(json)

(Also note that I'm using getlist for the 'id' parameter - that
enables you to get multiple values from GET parameters, when they are
supplied in the form /myurl/?id=1&id=2&id=3, which is the way browsers
automatically do it when submitting forms (rather than ?id=1,2,3).)
--
DR.

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

Reply via email to