views:
def new_authors(request): new_authors = UserProfile.get_new_authors_entries() # UserProfile.get_new_authors_entries() retrun: #[ # { # 'id': 4, # 'date_joined': datetime.datetime(2014, 9, 22, 13, 37, 25, 866974, tzinfo=<UTC>), # 'username': 'zzzzzz' # }, # { # 'id': 5, # 'date_joined': datetime.datetime(2014, 9, 22, 13, 37, 25, 866974, tzinfo=<UTC>), # 'username': 'wwwwww' # } #] return HttpResponse(json.dumps(new_authors), content_type='application/json')
models:
class UserProfile(User): @classmethod def get_new_authors_entries(self, cut_begin=0, cut_end=2): return self.objects.filter(is_active=1, is_superuser=0).values('id', 'date_joined', 'username').order_by('-date_joined')[cut_begin:cut_end]
как видите, из контроллера я делаю вызов функции модели User. эта функция возвращает набор записей. далее я пытаюсь сериализовать этот набор записей и передать в шаблон. но в результате получаю следующее сообщение об ошибке:
File “/usr/lib/python3.4/json/encoder.py”, line 173, in default
raise TypeError(repr(o) + “ is not JSON serializable”) TypeError: [{'id': 4, ‘date_joined’: datetime.datetime(2014, 9, 22, 13, 37, 25,
866974, tzinfo=<UTC>), ‘username’: ‘zzzzzz’}, {'id': 5, ‘date_joined’:
datetime.datetime(2014, 9, 22, 13, 37, 25, 866974, tzinfo=<UTC>),
‘username’: ‘wwwwww’}] is not JSON serializable