如何获取数据存储中不具有特定属性的实体

发布于 2024-11-30 15:50:35 字数 2235 浏览 1 评论 0原文

我正在尝试制定一个评估系统 这是我的课程,

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)

以下内容由员工提供,

class SubmitGoal(webapp.RequestHandler):
    def post(self):
      dtw = simplejson.loads(self.request.body)
      try:
        maxid = Goal.all().order("-GID").get().GID + 1
      except:
        maxid = 1
      try:
        g = Goal(GID=maxid, description=dtw[0], time=float(dtw[1]), weight=int(dtw[2]))
        g.put()
        self.response.out.write(simplejson.dumps("Submitted"))
      except:
        self.response.out.write(simplejson.dumps("Error"))

现在,经理在这里检查目标并批准与否..如果批准,则状态将在数据存储中存储为 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('AsmEmp =', not None)”,这只会返回那些具有我需要的 AsmEmp 属性的实体,反之亦然。

I'm trying to make an appraisal system
This is my class

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)

Following things are given by employee,

class SubmitGoal(webapp.RequestHandler):
    def post(self):
      dtw = simplejson.loads(self.request.body)
      try:
        maxid = Goal.all().order("-GID").get().GID + 1
      except:
        maxid = 1
      try:
        g = Goal(GID=maxid, description=dtw[0], time=float(dtw[1]), weight=int(dtw[2]))
        g.put()
        self.response.out.write(simplejson.dumps("Submitted"))
      except:
        self.response.out.write(simplejson.dumps("Error"))

Now, here Manager checks the goals and approve it or not.. if approved then status will be stored as true in datastore else 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"))

Now, this is where im stuck..."filter('status=',True)".. this is returning all the entities which has status true.. means which are approved.. i want those entities which are approved AND which have not been assessed by employee yet..

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

How am I supposed to do this? as I know that if I add another filter like "filter('AsmEmp =', not None)" this will only return those entities which have the AsmEmp attribute what I need is vice versa.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

⒈起吃苦の倖褔 2024-12-07 15:50:35

你显然不能这样做。正如文档所述:

无法查询缺少给定属性的实体。

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

You explicitly can't do this. As the documentation states:

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

Instead, create a property for is_assessed which defaults to False, and query on that.

顾挽 2024-12-07 15:50:35

当employee_assessed = db.user时,您不能简单地添加另一个字段吗?
并且仅在评估时填充此内容?

could you not simply add another field for when employee_assessed = db.user...
and only populate this at the time when it is assessed?

夜光 2024-12-07 15:50:35

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

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

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

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

The records do not lack the attribute in the datastore, it's simply set to None. You can query for those records with Goal.all().filter('status =', True).filter('AsmEmp =', None).

A few incidental suggestions about your code:

  1. 'Status' is a rather unintuitive name for a boolean.
  2. It's generally good Python style to begin properties and attributes with a lower-case letter.
  3. You shouldn't iterate over a query directly. This fetches results in batches, and is much less efficient than doing an explicit fetch. Instead, fetch the number of results you need with .fetch(n).
  4. A try/except with no exception class specified and no action taken when an exception occurs is a very bad idea, and can mask a wide variety of issues.

Edit: I didn't notice that you were using an Expando - in which case @Daniel's answer is correct. There doesn't seem to be any good reason to use Expando here, though. Adding the property to the model (and updating existing entities) would be the easiest solution here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文