问题描述
我想创建电影数据库,用户将能够标记他/她观看和喜欢的电影:
class Movies(ndb.Model):
watched = ndb.UserProperty()
喜欢= ndb.UserProperty()
这会起作用吗?我使用Google帐户。
我应该如何选择以后用户喜欢的所有电影?
更新。我遵循,并使用以下代码保存用户选择:
user = users.get_current_user()
如果用户:
userschoices = models.UsersChoices(
movie = ndb.Key(models.Movies,movie_id),#TODO:如果movie_id错误会怎么样?
watched = True,
user_id = user.user_id()
)
尝试:
userschoices.put()
self.response.out.write('1')
除外:
self.response.out.write('0')
但是,如果用户多次做出选择,则会将多条记录添加到数据存储中...
将用户ID和电影ID保存为关键字不会更好吗?
userschoices = models.UsersChoices.get_by_id( user.user_id()+' - '+ movie_id)
如果userschoices是None:
userschoices = models.UsersChoices(id = user.us er_id()+' - '+ movie_id)
userschoices.movie = ndb.Key(models.Movies,movie_id)#TODO:如果movie_id错误会怎么样?
userschoices.user_id = user.user_id()
if option =='likes':
userschoices.liked = True
elif option =='watched':
userschoices.watched = True
但是,如果我不通过喜欢
,那么它会用 None覆盖其值
(与已观看
相同,if没有通过, None使用
)。
两个不同的模型,一个存储所有 Movies
细节,另一个存储 UserChoices
:
class Movies(ndb.Model):
title = ndb.StringProperty(required = True)
director = ndb.StringProperty( )
whatever = ndb.StringProperty()
类UsersChoices(ndb.Model):
movie = ndb.KeyProperty(kind = Movies,required = True)
watched = ndb.BooleanProperty(required = True)
喜欢= ndb.BooleanProperty(required = True)
user_id = ndb.StringProperty(required = True)
@classmethod
def get_liked_movies(cls,user_id):
返回cls.query(cls.user_id == user_id,cls .liked == true).fetch(10)
@classmethod
def get_watched_movies(cls,user_id):
返回cls.query(cls.user_id == user_id,cls .watched == true).fetch(10)
@classmethod
def get_by(cls,user_id,movie_key):
返回cls.query(cls.user_id == user_id ,cls.movie == movie_key).get()
如果您需要存储关于用户的信息应该从,包含所有详细信息。 p $ p> class UserInfo(ndb.Model):
#Keyed by user_id
nickname = ndb.StringProper ty()
email = ndb.StringProperty()
创建一个新的 UserInfo
,你可以这样做:
from google.appengine.api import users
$ b $ user = users.get_current_user()
userinfo = UserInfo(
id = user.user_id(),
nickname = user.keyname(),
email = user.email()
)
userinfo.put()
然后,当用户登录时,使用他/她的 user_id
来检索观看/喜欢的电影。
from google.appengine.api导入用户
user = users.get_current_user()
userinfo = ndb.Key(UserInfo,user.user_id())。 get()
watched_movies = UsersChoices.get_watched_movies(userinfo.key.id())
liked_movies = UsersChoices.get_liked_movies(userinfo.key.id())
I would like to create movies database, where user will be able to mark movies he/she watched and liked:
class Movies(ndb.Model):
watched = ndb.UserProperty()
liked = ndb.UserProperty()
Will that work? I use Google accounts.How should I choose later all movies user liked?
Upd. I've followed systempuntoout approach and use the following code to save user choices:
user = users.get_current_user()
if user:
userschoices = models.UsersChoices(
movie=ndb.Key(models.Movies, movie_id), # TODO: what if movie_id is wrong?
watched=True,
user_id=user.user_id()
)
try:
userschoices.put()
self.response.out.write('1')
except:
self.response.out.write('0')
But if user makes his choice several times, then several records are added to the datastore...Wouldn't be it better just to save user id and movie id as keyname?
userschoices = models.UsersChoices.get_by_id(user.user_id() + '-' + movie_id)
if userschoices is None:
userschoices = models.UsersChoices(id=user.user_id() + '-' + movie_id)
userschoices.movie = ndb.Key(models.Movies, movie_id) # TODO: what if movie_id is wrong?
userschoices.user_id = user.user_id()
if option == 'liked':
userschoices.liked = True
elif option == 'watched':
userschoices.watched = True
However, with such approach if I don't pass liked
, then it overwrites its value with None
(the same with watched
, if not passed, None
is used).
I would go with two different Models, one that stores all the Movies
details and one to store the UserChoices
:
class Movies(ndb.Model):
title = ndb.StringProperty(required=True)
director = ndb.StringProperty()
whatever = ndb.StringProperty()
class UsersChoices(ndb.Model):
movie = ndb.KeyProperty(kind=Movies, required=True)
watched = ndb.BooleanProperty(required=True)
liked = ndb.BooleanProperty(required=True)
user_id = ndb.StringProperty(required=True)
@classmethod
def get_liked_movies(cls, user_id):
return cls.query(cls.user_id == user_id, cls.liked == true).fetch(10)
@classmethod
def get_watched_movies(cls, user_id):
return cls.query(cls.user_id == user_id, cls.watched == true).fetch(10)
@classmethod
def get_by(cls, user_id, movie_key):
return cls.query(cls.user_id == user_id, cls.movie == movie_key).get()
If you need to store informations about users you should create your UserInfo
Model, keyed by user_id
from the users API, with all the details Properties your application needs.
class UserInfo(ndb.Model):
#Keyed by user_id
nickname = ndb.StringProperty()
email = ndb.StringProperty()
To create a new UserInfo
, you could do:
from google.appengine.api import users
user = users.get_current_user()
userinfo = UserInfo(
id = user.user_id(),
nickname = user.keyname(),
email = user.email()
)
userinfo.put()
Then, when the user is logged in, use his/her user_id
to retrieve the watched/liked movies.
from google.appengine.api import users
user = users.get_current_user()
userinfo = ndb.Key(UserInfo, user.user_id()).get()
watched_movies = UsersChoices.get_watched_movies(userinfo.key.id())
liked_movies = UsersChoices.get_liked_movies(userinfo.key.id())
这篇关于如何构建电影数据库和用户选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!