2016-03-21 13 views
0

私はこのテーブルの各行の別のテーブルからフィールドを取る必要があるので、私はこのようなコードを書いているので、web2pyのビューからデータベーステーブルをクエリしようとしています:ビューからデータベースへのクエリ

{{for recipe in rows:}} 

<div class="well"> 
    <table> 
     <tr> 
      <td> 
      <div style="text-align:center"> 
<img width="200px" 
    src="{{=URL('download', args=db(db.uploads.recipe_id==recipe.id).select().first().up_file)}}" /> 
</div> 

      </td> 
      <td><button> 
      - 
      </button></td><td><span class='votes'>{{=recipe.votes}}</span></td><td><button> 
      + 
      </button><td><strong>{{=A("comments",_href=URL('view_posts',args=recipe.id))}},{{=recipe.name}}</strong></td></td></tr> 
    </table> 

</div> 
{{pass}} 

でも、ビューからデータベースにクエリを実行できるかどうかは疑問です。 コントローラから同じものをクエリしてビューに戻すにはどうすればよいですか? これは、愚かな間違いかもしれませんが、申し訳ありません私は

答えて

1

あなたをweb2pyのために新しいですあなたは、テーブル内の各行に別々のクエリを持つことになりますよう、それは、非常に効率的ではないということはなく、行うことができます。 rowsオブジェクトは、今すぐ登録を表しているので、

{{for row in rows:}} 

<div class="well"> 
    <table> 
     <tr> 
      <td> 
      <div style="text-align:center"> 
<img width="200px" 
    src="{{=URL('download', args=row.uploads.up_file)}}" /> 
</div> 

      </td> 
      <td><button> 
      - 
      </button></td><td><span class='votes'>{{=row.recipe.votes}}</span></td><td><button> 
      + 
      </button><td><strong>{{=A("comments",_href=URL('view_posts',args=row.recipe.id))}},{{=row.recipe.name}}</strong></td></td></tr> 
    </table> 

</div> 
{{pass}} 

注:

rows = db((your_current_query) & (db.uploads.recipe == db.recipe.id)).select() 

次にビューで:その代わり、コントローラでrowsオブジェクトを作成するクエリは、db.uploadsテーブルと結合を関与させるべきです2つのテーブルの間には、テーブル名とフィールド名の両方を使用して特定の値にアクセスする必要があります(row.nameではなくrow.recipe.nameなど)。そのことを明確にするために、forループでは、reciperowに変更しました。

関連する問題