1

我在将数据转储到 json 时遇到问题,我使用下面的代码,但在这一行出现异常

Result = json.dumps([{'jalaliDate': o.jalaliDate} for o in matchDays], cls=DjangoJSONEncoder)

MatchDays 值为[{'jalaliDate': u'1392/07/30'}]

我认为values("jalaliDate")我的过滤器有问题,因为当我删除它时,一切正常。

def calendar(request, year, month, day, hash):
    try:
        gregorianList = JalaliToGregorian(year, month, day).getGregorianList()
        startDate = datetime.date(gregorianList[0]-1,gregorianList[1],gregorianList[2])
        endDate = datetime.date(gregorianList[0]+1,gregorianList[1],gregorianList[2])
        matchDays = Match.objects.filter(matchDate__gt=startDate, matchDate__lt=endDate).values("jalaliDate").distinct()
        result = json.dumps([{'jalaliDate': o.jalaliDate} for o in matchDays], cls=DjangoJSONEncoder)
    except Exception:
        print Exception.message
    return result

这是控制台上的响应

0 errors found
October 23, 2013 - 16:18:04
Django version 1.5.2, using settings 'topelevensdjango.settings'
Development server is running at 127.0.0.1:800
Quit the server with CTRL-BREAK.
<attribute 'message' of 'exceptions.BaseException' objects>
[23/Oct/2013 16:18:14] "POST /json/ HTTP/1.1" 500 723

这是网上的回复

>>> jsonrpc.calendar(1392, 1, 1, 22)
Requesting ->
{"id":"jsonrpc", "params":[1392, 1, 1, 22], "method":"calendar", "jsonrpc":"1.0"}
Deferred(128, unfired)
Error ->
STATUS: 500
[object Object]
4

1 回答 1

0

最后我找到了对我使用的结果进行分组的答案,这是错误的

我已经替换了这段代码

matchDays = Match.objects.filter(matchDate__gt=startDate, matchDate__lt=endDate).values("jalaliDate").distinct()

使用此代码

matchDays = Match.objects.filter(matchDate__gt=startDate, matchDate__lt=endDate)
matchDays.query.group_by = ['jalaliDate']

问题就解决了

于 2013-10-24T17:21:03.750 回答