GAE: KindError when getting entities via ListPrope

2019-08-15 17:33发布

I have a Contest entity which uses a ListProperty to store keys of Candidate entities. Here are some details:

class Contest(db.Model):
    candidates = db.ListProperty(db.Key)

    def create_candidate_objects(self):
        put_list = []
        for n, name in enumerate(self.tmp_candidates):
            put_list.append(Candidate(parent = self, name = name))
        keys = db.put(put_list)
        self.candidates = keys
        self.put()

class Candidate(db.Model):
    name = db.StringProperty(required = True)

When I do this query:

c = models.Candidate.all().ancestor(contest).fetch(2)

Everything works fine.

But when I do a query using the ListProperty like this:

c = db.get(contest.candidates)

I get the error

KindError: No implementation for kind 'Candidate'

Can you help me understand why the last query does not work? I have imported the Candidate class into the module that is doing the query.

UPDATE: Note that his is on the dev server in case that might make a difference.

UPDATE2:

As suggested by Nick, this works:

c2 = models.Candidate.get(contest.candidates)

but this does not work (raises KindError):

from models import Candidate
c = db.get(contest.candidates)

UPDATE3:

The offending line occurs in test code so I can't try this particular failure on the production server. I have used the exact same line elsewhere and it does work on both the dev and production servers.

Nick asked about imports, and I am doing this in my test code:

sys.path.append("/usr/local/google_appengine/lib/simplejson")
import simplejson as json

but everything else is just typical.

2条回答
放荡不羁爱自由
2楼-- · 2019-08-15 17:58

Try this:

c = db.get([str(i) for i in contest.candidates])
查看更多
祖国的老花朵
3楼-- · 2019-08-15 18:06

Most likely you're defining Candidate in another module you haven't imported. Where is Candidate defined? Have you imported the module it's in when you call db.get? Calling Candidate.get(key_list) should work, since it forces you to have a valid Candidate when you call it.

查看更多
登录 后发表回答