Created
October 14, 2013 04:05
-
-
Save anonymous/6970591 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Resources.py | |
class MakeResource(ModelResource): | |
#make = fields.CharField(attribute='make') | |
class Meta: | |
queryset = Vehicle.objects.all() | |
#queryset = serializers.serialize("json",Vehicle.objects.values('make') .distinct()[1:2]) | |
#prep = Vehicle.objects.values('make') .distinct()[1:2] | |
#queryset = Struct(prep) | |
resource_name = 'make' | |
allowed_methods = ['get'] | |
#serializer = Serializer() | |
# list(queryset) | |
filtering = { | |
'make': ALL, | |
} | |
def apply_filters(self, request, applicable_filters): | |
""" | |
An ORM-specific implementation of ``apply_filters``. | |
The default simply applies the ``applicable_filters`` as ``**kwargs``, | |
but should make it possible to do more advanced things. | |
Here we override to check for a 'distinct' query string variable, | |
if it's equal to True we apply distinct() to the queryset after filtering. | |
""" | |
distinct = request.GET.get('distinct', False) == 'True' | |
if distinct: | |
return self.get_object_list(request).filter(**applicable_filters).distinct() | |
#return self.get_object_list(request).distinct(filter(**applicable_filters)) #.distinct() | |
else: | |
return self.get_object_list(request).filter(**applicable_filters) | |
# model snippet models.py | |
class Vehicle(models.Model): | |
make= models.CharField(max_length=50, db_index=True) | |
model= models.CharField(max_length=50 ,db_index=True) | |
# urls.py | |
admin.autodiscover() | |
v1_api = Api(api_name='v1') | |
v1_api.register(CarDataResource()) | |
v1_api.register(MakeResource()) | |
#cardata_resource= CarDataResource() | |
#make_resource= MakeResource() | |
urlpatterns = patterns('', | |
# Examples: | |
# url(r'^$', 'opendata.views.home', name='home'), | |
# url(r'^blog/', include('blog.urls')), | |
url(r'^admin/', include(admin.site.urls)), | |
# url(r'^api/', include(cardata_resource.urls)), | |
#url(r'^api/', include(make_resource.urls)), | |
url(r'^api/', include(v1_api.urls)), | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment