如何构建电影数据库和用户选择? [英] How to structure movies database and user choices?

查看:47
本文介绍了如何构建电影数据库和用户选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建电影数据库,用户可以在其中标记他/她观看和喜欢的电影:

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?

更新.我遵循了systempuntoout方法并使用以下代码来保存用户选择:

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')

但是如果用户多次做出选择,那么就会向数据存储中添加几条记录...将用户 id 和电影 id 保存为 keyname 不是更好吗?

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

但是,如果我不通过 liked 的这种方法,那么它会用 None 覆盖它的值(与 watched 相同,如果未通过,则使用 None).

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).

推荐答案

我会使用两种不同的模型,一种用于存储所有 Movies 详细信息,另一种用于存储 UserChoices:

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()

如果您需要存储有关用户的信息,您应该创建您的 UserInfo 模型,由 users API,包含您的应用程序所需的所有详细属性.

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()

要创建一个新的UserInfo,你可以这样做:

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()

然后,当用户登录时,使用他/她的user_id来检索观看/喜欢的电影.

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())

这篇关于如何构建电影数据库和用户选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆