Моя строка кода выглядит следующим образом:
replies = Comment.objects.filter(reply_to__pk__exact=pk).annotate(dates=timesince.timesince('date'))\
.order_by('-dates')\
.values_list("owner__username", "text", "dates", "likes", "owner__customuser__picture")
Я хочу, чтобы столбец дат в результате преобразовывался функцией timesince.timesince
. Вместо этого он выдает ошибку примерно так:
AttributeError: 'str' object has no attribute 'year'
Как мне решить эту проблему?
РЕДАКТИРОВАТЬ 1: На основе запроса @GrandPhuba обратная связь после реализации второго решения составляет
Traceback (most recent call last):
File "/home/lord-sarcastic/.local/share/virtualenvs/Bonychicken-xy3Z_xpD/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/lord-sarcastic/.local/share/virtualenvs/Bonychicken-xy3Z_xpD/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/lord-sarcastic/.local/share/virtualenvs/Bonychicken-xy3Z_xpD/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/lord-sarcastic/Bonychicken/main/views.py", line 374, in get_replies
print(comments)
File "/home/lord-sarcastic/.local/share/virtualenvs/Bonychicken-xy3Z_xpD/lib/python3.8/site-packages/django/db/models/query.py", line 252, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/lord-sarcastic/.local/share/virtualenvs/Bonychicken-xy3Z_xpD/lib/python3.8/site-packages/django/db/models/query.py", line 276, in __iter__
self._fetch_all()
File "/home/lord-sarcastic/.local/share/virtualenvs/Bonychicken-xy3Z_xpD/lib/python3.8/site-packages/django/db/models/query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/lord-sarcastic/.local/share/virtualenvs/Bonychicken-xy3Z_xpD/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1095, in apply_converters
value = converter(value, expression, connection)
File "/home/lord-sarcastic/.local/share/virtualenvs/Bonychicken-xy3Z_xpD/lib/python3.8/site-packages/django/db/backends/sqlite3/operations.py", line 260, in convert_datetimefield_value
value = parse_datetime(value)
File "/home/lord-sarcastic/.local/share/virtualenvs/Bonychicken-xy3Z_xpD/lib/python3.8/site-packages/django/utils/dateparse.py", line 107, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or bytes-like object