2
我構建了一個django-tastypie api.It返回數據,當我打到網址。 我想根據日期對數據進行排序。問題是它的一個字符型字段排序爲非orm(mongodb)的tastypie
資源代碼:
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
網址命中返回:
[{
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/"
}]