如何从模型访问属性到Django Queryset?

时间:2015-03-27 05:24:54

标签: python django django-queryset

我有这个模型,我在其中创建了一个格式化DateTimeField的属性。但是我无法访问/引用它。到目前为止试过这个:

class DataForecast(models.Model):
    date_time = models.DateTimeField()
    rainfall_mm_per_10_min = models.FloatField(null=True)
    wl_observed_m = models.FloatField(null=True)
    wl_forecasted_m = models.FloatField(null=True)

    def __unicode__(self):
        return self.date_time

    @property
    def date_time_formatted(self):
        import datetime
        return self.date_time.strftime('%b %d, %Y %I:%M %p')

而在我的views.py中:

formatted = [dataforecast.date_time_formatted for dataforecast in DataForecast.objects.all()]
getall = DataForecast.objects.all().values_list(formatted, "rainfall_mm_per_10_min", "wl_observed_m","wl_forecasted_m")
return HttpResponse(json.dumps(list(getall),cls=DjangoJSONEncoder), content_type='application/json')

它返回错误:

  

异常值:'list'对象没有属性'split'

2 个答案:

答案 0 :(得分:1)

有两种方法可以做到这一点:

添加.values()

DataForecast.objects.all().values()

会给你一个列表来迭代。

否则请记住输出是一个对象,因此使用object.key表示法来获取值

答案 1 :(得分:0)

values/values_list仅使用可选字段参数。他们不会将查询集作为参数。

将unicode设置为返回值

def __unicode__(self):
    return unicode(self.date_time)

你可以直接这样做

In [6]: DataForecast.objects.all().values("date_time", "rainfall_mm_per_10_min", "wl_observed_m","wl_forecasted_m")
Out[6]: [{'date_time': datetime.datetime(2015, 3, 27, 6, 5, 36, tzinfo=<UTC>), 'wl_forecasted_m': 2.0, 'rainfall_mm_per_10_min': 1.0, 'wl_observed_m': 2.0}, {'date_time': datetime.datetime(2001, 1, 1, 0, 0, tzinfo=<UTC>), 'wl_forecasted_m': None, 'rainfall_mm_per_10_min': None, 'wl_observed_m': None}]

或者更好的尝试

In [7]: DataForecast.objects.all().values()
Out[7]: [{'wl_observed_m': 2.0, 'date_time': datetime.datetime(2015, 3, 27, 6, 5, 36, tzinfo=<UTC>), 'wl_forecasted_m': 2.0, u'id': 5, 'rainfall_mm_per_10_min': 1.0}, {'wl_observed_m': None, 'date_time': datetime.datetime(2001, 1, 1, 0, 0, tzinfo=<UTC>), 'wl_forecasted_m': None, u'id': 6, 'rainfall_mm_per_10_min': None}]

转换为json

In [19]: all = DataForecast.objects.all().values()

In [20]: all
Out[20]: [{'wl_observed_m': 2.0, 'date_time': datetime.datetime(2015, 3, 27, 6, 5, 36, tzinfo=<UTC>), 'wl_forecasted_m': 2.0, u'id': 5, 'rainfall_mm_per_10_min': 1.0}, {'wl_observed_m': None, 'date_time': datetime.datetime(2001, 1, 1, 0, 0, tzinfo=<UTC>), 'wl_forecasted_m': None, u'id': 6, 'rainfall_mm_per_10_min': None}]

In [21]: for i in all: i['date_time'] =  i['date_time'].strftime("%b %d %Y %H:%M:%S")

In [22]: all
Out[22]: [{'wl_observed_m': 2.0, 'date_time': 'Mar 27 2015 06:05:36', 'wl_forecasted_m': 2.0, u'id': 5, 'rainfall_mm_per_10_min': 1.0}, {'wl_observed_m': None, 'date_time': 'Jan 01 2001 00:00:00', 'wl_forecasted_m': None, u'id': 6, 'rainfall_mm_per_10_min': None}]

In [23]: import json

In [25]: json.dumps(list(all))                                                                                                                                                  
Out[25]: '[{"wl_observed_m": 2.0, "date_time": "Mar 27 2015 06:05:36", "wl_forecasted_m": 2.0, "id": 5, "rainfall_mm_per_10_min": 1.0}, {"wl_observed_m": null, "date_time": "Jan 01 2001 00:00:00", "wl_forecasted_m": null, "id": 6, "rainfall_mm_per_10_min": null}]'