如何获取数据中没有特定属性的实体

2024-10-02 16:22:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想建立一个评估系统 这是我的课

class Goal(db.Expando):
  GID = db.IntegerProperty(required=True)
  description = db.TextProperty(required=True)
  time = db.FloatProperty(required=True)
  weight = db.IntegerProperty(required=True)
  Emp = db.UserProperty(auto_current_user=True)
  Status = db.BooleanProperty(default=False)

以下内容由员工提供

^{pr2}$

现在,经理检查目标并批准与否。。如果批准,则状态将在数据存储中存储为true,否则为false

idsta = simplejson.loads(self.request.body)
    try:
        g = db.Query(Goal).filter("GID =", int(idsta[0])).get()
        if g:
            if idsta[1]:
                g.Status=True
                try:
                    del g.Comments
                except:
                    None
            else:
                g.Status=False
                g.Comments=idsta[2]
            db.put(g)
            self.response.out.write(simplejson.dumps("Submitted"))
    except:
        self.response.out.write(simplejson.dumps("Error"))

现在,这就是我卡住的地方…“过滤器('status=',True)”。。这将返回所有状态为true的实体。。经批准的方式。。我想要那些被批准的,还没有被员工评估的实体。。在

def get(self):
    t = []
    for g in Goal.all().filter("Status = ",True):
        t.append([g.GID, g.description, g.time, g.weight, g.Emp])
    self.response.out.write(simplejson.dumps(t))
def post(self):
    idasm = simplejson.loads(self.request.body)
    try:
        g = db.Query(Goal).filter("GID =", int(idasm[0])).get()
        if g:
            g.AsmEmp=idasm[1]
            db.put(g)
            self.response.out.write(simplejson.dumps("Submitted"))
    except:
        self.response.out.write(simplejson.dumps("Error"))

我该怎么做?我知道,如果我添加另一个过滤器,比如“filter('asemp=',not None)”,这只会返回那些具有asemp属性的实体,反之亦然。在


Tags: selftruedbresponsestatusrequiredfilterout
3条回答

记录并不缺少数据存储中的属性,它只是设置为“无”。您可以使用Goal.all().filter('status =', True).filter('AsmEmp =', None)查询这些记录。在

关于代码的一些附带建议:

  1. “Status”是布尔值的一个相当不直观的名称。在
  2. 通常好的Python风格是以小写字母开头的属性和属性。在
  3. 不应该直接迭代查询。这将成批获取结果,并且比显式获取的效率低得多。相反,使用.fetch(n)获取所需的结果数。在
  4. try/except不指定异常类,并且在异常发生时不采取任何操作,这是一个非常糟糕的主意,它可以掩盖各种各样的问题。在

编辑:我没注意到你在用Expando——在这种情况下,@Daniel的答案是正确的。不过,在这里使用Expando似乎没有什么好的理由。将属性添加到模型(并更新现有实体)将是这里最简单的解决方案。在

你显然不能这么做。正如documentation所述:

It is not possible to query for entities that are missing a given property.

相反,请为is_assessed创建一个默认为False的属性,并对其进行查询。在

您不能简单地为“当员工评估=数据库用户... 只在评估时填写?在

相关问题 更多 >