Assemble a db query from GET requests

I'm trying to assemble a query from values supplied through GET requests, is this at all possible? Here's an example of what I'm hoping to do:

def kml(request):
    venues = Venue.objects.filter(
        (Q(neighborhood__city__metro__slug=request.GET.get('metro')) if request.GET.get('metro')),
        (Q(neighborhood__slug=request.GET.get('neighborhood')) if request.GET.get('metro'))
    )
    return HttpResponse(venues)

Basically, if I go to foo.com/?metro=nyc it'd return all Venue objects for that Metropolitan Area. If I only supply the neighborhood foo.com/?neighborhood=soho, it'd return all venues for any neighborhood named Soho. Finally, foo.com/?metro=nyc&neighborhood=soho would supply any venue meeting both the Metropolitan Area and Neighborhood criteria.

Is this possible?

ps. Is this essentially what an REST api does? Am I better off trying to use Piston, TastyPie, or Django-REST?

Answers


Django query api is chainable, so you can do this:

def kml(request):
    venues = Venue.objects

    if request.GET.get('metro'):
       venues = venues.filter((Q(neighborhood__city__metro__slug=request.GET.get('metro')))

     if request.GET.get('metro')
        venues = venues.filter(Q(neighborhood__slug=request.GET.get('neighborhood')) )

     # and so on...

Need Your Help

symfony/validator composer package not loading correctly

php symfony2 composer-php

I'm using the symfony/validator as project dependencies. But I can't get the validator classes in the vendor/autoload.php. When I looked in the vendor/symfony/validator folder I got the dir structure

Extracting page titles and contributors from MediaWiki XML

python xml-parsing python-2.7 mediawiki elementtree

I have a very large (7GB) MediaWiki XML dump, which consists of records of each change made to each page of the Wiki. I am trying to record which users have contributed to each page, and so I want to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.