Monday, February 24, 2014

How to return Random items from a Mongodb Resource

Hi Friends,

I need to return random items from a tastypie resource which is a resources.MongoEngineResource.

I have already tried using MyClass.objects.all().order_by('?'), but it simply keep returning the same rows always. Following is the code of my resource.

class MViewSubResponseRandomResource(resources.MongoEngineResource):
    class Meta:
        queryset = SubResponse.objects.all().filter(asset_type='Photo').order_by('?')
        resource_name = 'mview/random'
        allowed_methods = ['get']
        authentication = SillyAuthentication()
        authorization = SillyAuthorization()

    def dehydrate(self, bundle):
        resource_uri = self.get_resource_uri(bundle)
        bundle.data = {}
        bundle.data['id']=bundle.obj.id
        bundle.data['file_url']=bundle.obj.file_url
        bundle.data['action_type']=bundle.obj.action_type
        bundle.data['asset_type']=bundle.obj.asset_type
        bundle.data['resource_uri']=resource_uri
        bundle.data['end_date']=bundle.obj.end_date
        return bundle
    
    def get_object_list(self, request):
        workgroup_id=request.REQUEST['wid']
        superset_limit=1000
        subresponses = SubResponse.objects.all().filter(asset_type='Photo', rejected=False, work_group_id=workgroup_id).order_by('?')[:superset_limit]
        return subresponses



Regards,
Anuj

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/36b02a36-4292-4bf9-8647-5fcf4672d84f%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment