Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong></p> <p>I've adapted the apply_filters method linked below to use the values before making the <code>distinct</code> call. </p> <pre><code>def apply_filters(self, request, applicable_filters): qs = self.get_object_list(request).filter(**applicable_filters) values = request.GET.get('values', '').split(',') if values: qs = qs.values(*values) distinct = request.GET.get('distinct', False) == 'True' if distinct: qs = qs.distinct() return qs </code></pre> <p><code>values</code> returns dictionaries instead of model objects, so I don't think you need to override <code>alter_list_data_to_serialize</code>. </p> <p><strong>Original response</strong></p> <p>There is a nice solution to the <code>distinct</code> part of the problem <a href="https://groups.google.com/forum/?fromgroups=#!topic/django-tastypie/YWyeYjmhqUQ" rel="nofollow">here</a> involving a light override of <code>apply_filters</code>.</p> <p>I'm surprised I'm not seeing a slick way to filter which fields are returned, but you could implement that by overriding <a href="http://django-tastypie.readthedocs.org/en/latest/resources.html#alter-list-data-to-serialize" rel="nofollow"><code>alter_list_data_to_serialize</code></a> and deleting unwanted fields off the objects just before serialization.</p> <pre><code>def alter_list_data_to_serialize(self, request, data): data = super(PersonResource, self).alter_list_data_to_serialize(request, data) fields = request.GET.get('fields', None) if fields is not None: fields = fields.split(',') # Data might be a bundle here. If so, operate on data.objects instead. data = [ dict((k,v) for k,v in d.items() if k in fields) for d in data ] return data </code></pre> <p>Combine those two to use something like <code>/api/v1/person/?distinct=True&amp;values=first_name</code> to get what you're after. That would work generally and would still work with additional filtering (<code>&amp;last_name=Jones</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload