简体   繁体   English

如何在GAE中检查记录的存在性

[英]How do I check for Existence of a Record in GAE

I am trying to create a simple view in Django & GAE, which will check if the user has a profile entity and prints a different message for each case. 我试图在Django和GAE中创建一个简单的视图,它将检查用户是否有一个配置文件实体并为每个案例打印不同的消息。 I have the program below, but somehow GAE always seem to return a object. 我有下面的程序,但不知何故,GAE似乎总是返回一个对象。 My program is below 我的节目如下

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

My model object is Profile defined as follows: 我的模型对象是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)

Thanks for the help. 谢谢您的帮助。

I am afraid you forgot to execute the query. 我担心你忘了执行查询。 Try the get() method. 试试get()方法。 It returns the first result, or None if the query returned no results. 它返回第一个结果,如果查询没有返回结果,则返回None。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM