python/ google-app-engine/ google-cloud-datastore

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.

您不能简单地为employee_assesssed = db.user ...添加另一个字段,而仅在评估时填充它吗?

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.

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question How to get xml elements which have childs with a certain tag and attribute How do I query datastore entities that have any value for a certain property efficiently? How can I web scrape certain words that don't have an attribute attached to them? Django - Get objects that don't have one to to one attribute How to get element which don't have a class name and a id using selenium webdriver get entities for last 7 days in datastore Why don`t I have attribute textinput? Get the column names which don't have Date in them using python? How to select tags that have certain attribute type How to ignore xml children that have a certain attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM