python - Annotate with latest related object in Django -


i have model conversation , model message.

the model message has foreign key conversation, text field, , date field.

how can list conversations , each conversation recent message , date of recent message?

i guess it's like

conversation.objects.annotate(last_message=max('messages__date')) 

but give me latest date. want last_message contain both text of last message , date created. maybe need use prefetch_related?

you can prefetch_related , queryset. like:

conversation.objects.prefetch_related(prefetch('messages'),              queryset=message.objects.order_by('date')[0]) 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -