开发者

how to get entities which don't have certain attribute in datastore

开发者 https://www.devze.com 2023-03-29 23:49 出处:网络
I\'m trying to make an appraisal system This is my class class Goal(db.Expando): GID = db.IntegerProperty(required=True)

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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消