1

I have a django project and I am using tastypie to turn my app into a REST based API.

I let users upload photographs and then other registered users can like them. I want to prevent a single from liking a photo more than once.

I know that using views or tastypie specific code I could add functionality to check if a user has already like a photograph and if they have then not increment the count. But I was wondering if there is a way to do this check within the model (and if that is good or bad)

Thanks

4

1 に答える 1

0

好きなモデルがいるといいのですが?そうでない場合は、 M2M ユーザー <-> 写真関係の中間モデルとして作成します。次に、単純な一意の制約を追加します。

unique_together = ("user", "photo")

これはデータベース レベルで適用されるため、優れたパフォーマンスが得られます。

いいねを保持するための専用モデルが必要ない場合は、残念ながら私は助けることができません。ユーザーが写真を気に入ったという事実を保存するエレガントな方法は他にありません。最後に、後で簡単に追加できる追加機能があるため、M2M に満足するはずです。

幸運を!

于 2013-07-05T07:59:56.937 に答える