1. Jesper Noehr
  2. django-piston
  3. Issues
Issue #32 new

Convert GET string args to kwargs to pass to the emitter

Jason Christa
created an issue

It would be a nice enhancement if on the GET method all the GET string arguments were coerced into the kwargs and passed along to the emitter.

So http://api.example.com/cars?year=2009 would end up running Car.objects.filter(year=2009).

Comments (2)

  1. Jason Christa reporter

    This is a rough draft of a way you could extend a Handler class's read method and use the GET string in as queryset arguments:

    def read(self, request, *args, **kwargs):
            def format_qs(q):
                """Formats a query into Django QuerySet selectors"""
                kw = {}
                for k,v in q.items():
                    if k.endswith('__in') and isinstance(v, basestring):
                        v = v.split(',')
                    kw[str(k)] = v
                return kw
            if not self.has_model():
                return rc.NOT_IMPLEMENTED
            pkfield = self.model._meta.pk.name
            query = dict(request.REQUEST.items())
            fields = query.pop('fields', None)
            exclude = query.pop('exclude', None)
            offset = query.pop('offset', 0)
            limit = query.pop('limit', None)
            order_by = query.pop('order_by', None)
            if fields:
                self.fields = fields.split(',')
            if exclude:
                self.exclude = exclude.split(',')
            if pkfield in kwargs:
                    return self.model.objects.get(pk=kwargs.get(pkfield))
                except ObjectDoesNotExist:
                    return rc.NOT_FOUND
                except MultipleObjectsReturned: # should never happen, since we're using a PK
                    return rc.BAD_REQUEST
                if args[0] == '':
                    args = ()
                query_set = self.model.objects.filter(*args, **kwargs)
                if order_by:
                    query_set = query_set.order_by(*order_by.split(','))
                if limit:
                    query_set = query_set[offset:limit]
                return query_set
  2. Anonymous

    This is a really nice enhancement to allow django-ish queries!

    Probably prepending a _ before the magic query items so that they won't clash with the potential fields.


  3. Log in to comment