python - 如何在 GAME 中检查记录是否存在

标签 python django google-app-engine django-models

我正在尝试在 Django 和 GAE 中创建一个简单的 View ,它将检查用户是否有个人资料实体并为每种情况打印不同的消息。我有下面的程序,但不知何故,GAE 似乎总是返回一个对象。我的程序如下

import datetime
from django.http import HttpResponse, HttpResponseRedirect
from google.appengine.api import users
from google.appengine.ext import db
from models import Profile
import logging
#from accounts.views import profile

# Create your views here.
def login_view(request):
    user = users.get_current_user()
    profile = db.GqlQuery("SELECT * FROM Profile WHERE account = :1",
                            users.get_current_user())
    logging.info(profile)
    logging.info(user)
    if profile:
        return HttpResponse("Congratulations Your profile is already created.")
    else:
        return HttpResponse("Sorry Your profile is NOT created.")

我的模型对象 Profile 定义如下:

class Profile(db.Model):
    first_name = db.StringProperty()
    last_name = db.StringProperty()
    gender = db.StringProperty(choices=set(["Male", "Female"]))
    account = db.UserProperty(required = True)
    friends = db.ListProperty(item_type=users.User)
    last_login = db.DateTimeProperty(required=True)

感谢您的帮助。

最佳答案

恐怕您忘记执行查询了。试试 get()方法。它返回第一个结果,如果查询没有返回结果,则返回 None。

profile = db.GqlQuery("SELECT * FROM Profile WHERE account = :1",
                        users.get_current_user()).get()

关于python - 如何在 GAME 中检查记录是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2751345/

相关文章:

python - 如何为类型参数设置 python 类型

python - h5py 不遵守分块规范?

python - Django 。登录后重定向用户

django - models.py(Django/Python)中的声明顺序是否重要?

java - 使用 GAE 文件服务 API

python - 随机不重复?

python - Django,Modelform不呈现

google-app-engine - 升华 : No code completion for App Engine packages

python - 如何在 AppEngine 中获取所有实体类型

python - Django:Gmail SMTP 错误:请先运行 connect()