私はSqlalchemyとPostgreSQLを使用しています。私のモデルでは、Product
とImage
があります。私は各製品に複数の画像を持たせたい。これは商品の提出方法です:トランザクションモジュールからトランザクション結果を取得する
def submit_product(self, **kwargs):
product = Product(
title=kwargs['title'],
sub_category_id=kwargs['sub_category_id'],
year_of_production=kwargs['year_of_production'],
country_of_production=kwargs['country_of_production'],
quantity=kwargs['quantity'],
price=kwargs['price'],
account_id=session.get('account_id', None)
)
DBSession.add(product)
transaction.commit()
for image in kwargs['images']:
image = Image(
product_id= # what should I put here?,
image=image.file.read()
)
DBSession.add(image)
transaction.commit()
最後の部分では、画像をdbに追加しています。画像を設定するにはproduct_id
が必要です。しかし、私はそれを得る方法を見つけることはありません。新しい商品がどのIDに割り当てられているかを知るために、トランザクションの結果を得る方法はありますか? DBSession.add()
およびtransaction.commit()
はともにNone
を返します。
transaction.commit()
を変更がコミットされていますので、あなたが、product.id
を使用することができます使用した後
なぜ私は神のためにこれを試してみませんでしたか? :D tnx :) – Juggernaut
他のORMを使用している場合は、コミットすることでこの値が得られるかもしれません。問題ない。 ;) – turkus
@AminEtesamianなぜあなたは納得を拒否しましたか? – turkus