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}}

しかし、ビューからデータベースにクエリを実行できるかどうかは疑問です。そうでない場合、どうすればコントローラーから同じものを照会してビューに返すことができますか? これはばかげた疑問かもしれませんが、申し訳ありませんが、私は web2py を初めて使用します

4

1 に答える 1

1

それを行うことはできますが、テーブルの行ごとに個別のクエリが必要になるため、あまり効率的ではありません。代わりにrows、コントローラーでオブジェクトを作成するためのクエリには、db.uploadsテーブルとの結合が含まれている必要があります。

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

次に、ビューで:

{{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}}

オブジェクトが 2 つのテーブル間の結合を表すようになったためrows、特定の値にアクセスするにはテーブル名とフィールド名の両方を使用する必要があります (例: ではrow.recipe.nameなくrow.name)。それを明確にするために、forループで に変更recipeしましたrow

于 2016-03-21T17:26:56.453 に答える