1👍
✅
Give this a try
from django.db.models import F
Comment.objects.filter(news_id=news_id).values("nid", "content", "parent_comment_id", "create_time", username=F('user__username'))
0👍
If user is foreign key relation with models.user , then you can access username as user.username.
And for create_time , it depends on the datatype you have configured for it in models.py file.
If it is datetime object then you can convert it into string using datetime.strftime() as create_time.strftime("%m/%d/%Y, %H:%M:%S")
- [Answered ]-Python Django models id increment by 2
- [Answered ]-Django set environment variable via command line and use in Migrations
- [Answered ]-Django inclusion tag takes_context TemplateDoesNotExist error
0👍
if in Comment model user is ForeignKey, you can use user__username
to get the value, if you whant to change user__username to user, you can try this:
list(Comment.objects.filter(news_id=news_id).values("nid", "content", "parent_comment_id", user='user__name', "create_time"))
👤Max
0👍
ret = list(Comment.objects.filter(news_id=news_id).values("nid", "content", "parent_comment_id", "user__username", "create_time"))
This query will show username with key as user__username.
- [Answered ]-Django how to link to some part of a page
- [Answered ]-Searching features implementation in django project
- [Answered ]-ORA-00904: "2022-01-20": invalid identifier
- [Answered ]-Sending localized messages to Django users
Source:stackexchange.com