[Answered ]-How to adjust Django query set value?

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")

👤P-DOX

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.

👤P-DOX

Leave a comment