Jin-hyeong Ma
Jin-hyeong Ma

Reputation: 79

JOIN on django orm

1. 1.1. query: QuestionRequestAnswerModel.objects.filter(request_no__exact=index['id']).select_related('answer_user').values('answer_user', 'amount', 'select_yn') enter image description here

2. 2.1. query: QuestionRequestAnswerModel.objects.filter(request_no__exact=index['id']).select_related('answer_user')[0].answer_user

enter image description here

  1. description I want to get answer_user's last_name. but when I use django orm by select_realated('answer_user'), the result give me the answer_user's id only.

how can I get another column, except id, on ORM join?

Upvotes: 0

Views: 81

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32244

You can follow foreign key relationships in value queries by using the double underscore notation

QuestionRequestAnswerModel.objects.filter(
    request_no__exact=index['id']
).select_related(
    'answer_user'
).values(
    'answer_user__last_name',
    'amount',
    'select_yn'
)

Upvotes: 1

Related Questions