Django显示外键的名称而不是外键id

时间:2016-12-01 10:26:47

标签: django

如何从查询中显示外键的名称?

<div class="container">
<table>
	<thead>
	<tr>
    	<th width="120px">First</th>
        <th width="120px">Second</th>
 <th width="120px">Third</th>
        <th width="120px">Fourth</th>
        <th width="120px">Fifth</th>
    </tr>
    
    </thead>
</table>
<div class="scroallable_div">
<table>
            <thead>
            <tr>
                <th width="120px"></th>
                <th width="120px"></th>
                <th width="120px"></th>
                <th width="120px"></th>
            </tr>
            </thead>
    <tbody>
    		<tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
            <tr>
            		<td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
                    <td>hgdfgdgfd</td>
            
            </tr>
    
    </tbody>

</table>
</div>
</div>

如何使查询显示类别名称而不是category_id?

谢谢

1 个答案:

答案 0 :(得分:0)

Books.objects.filter(user=4).values('category__name').annotate(amount2=Sum('amount'))