简体   繁体   English

排序中的非ormable(mongodb)

[英]Sorting in tastypie for non-orm(mongodb)

I have constructed an django-tastypie api.It returns data when i hit the url. 我构建了一个django-tastypie api。当我点击url时它会返回数据。 I want the data to be sorted based on date. 我希望根据日期对数据进行排序。 The problem is its a character field 问题是它的字符字段

Resource Code: 资源代码:

class myResource(Resource):
  date = fields.CharField(attribute='date', default=None)
  time = fields.CharField(attribute='time', default=None)
  myid = fields.FloatField(attribute='newsid', default=None)

  class Meta:
    resource_name = 'data'
    object_class = dict2obj

  def obj_get_list(self, bundle, **kwargs):
    bundle = []

    #get_data fetches data from mongodb

    content = self.get_data()

    #content is a dict    

    for key, value in content.items():
      value['myid'] = key
      bundle.append(dict2obj(value))

    #bundle is a list

    return bundle

Url Hit Returns: 网址点击返回:

    [{
        date: "2013-09-24 08:56:00.000",
        myid: 266131,
        time: "08:56"
    },
    {
        date: "2013-09-24 09:43:00.000",
        myid: 266151,
        resource_uri: "/api/eboss/news/266151/"

    }]

I don't know your dict2obj , but if date and time are really date and time, then using tastypie.fields.DateField and tastypie.fields.TimeField respectively are more appropriate. 我不知道您的dict2obj ,但是如果datetime确实是日期和时间,那么分别使用tastypie.fields.DateFieldtastypie.fields.TimeField更为合适。

To do sorting, You can override apply_sorting(obj_list, options=None) function, where options is dictionary containing query parameters (GET method). 要进行排序,您可以覆盖apply_sorting(obj_list, options=None)函数,其中options是包含查询参数的字典(GET方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM