Getting attributeError: type object 'Level_2_Headings' has no attribute 'ancestor' (GAE datastore)

StackOverflow https://stackoverflow.com/questions/14146705

Вопрос

I'm using GAE, Datastore, Python 2.7 and am creating/updating entries in the datastore using ancestors for the first time. Here I'm trying to see if there are any entries/rows in Level_2_Headings datastore and if there are not, then creating a new one. If one does exist, updating the description. I'm getting this error -- the datastore for Level_2_Headings is empty so it should be bringing back empty so that I can add the new entry, but instead, I get an error in the query for the q2 object where I use the ancestor attribute - any ideas on why this would be when I'm expecting just an empty object to be returned since it doesn't exist in the datastore??? Help is appreciated as usual.

if q:
    q2 = Level_2_Headings.ancestor(q.key()).filter("name2 =",heading2).get()
    if q2:
        q2.description2 = description2
        q2.put()        
    else:           
        #new level 2 being added to ds
        new_2 = Level_2_Headings(parent=q2, name2=name2, description2=description2, heading_type=heading_type)
        new_2.put()

        message="Added NEW category entry to level 2"
Это было полезно?

Решение

You are referencing the Level_2_Heading model directly, without calling all() to retrieve the records. Level_2_Headings.all() will return an object that has the ancestor attribute, so try changing your first q2 to:

q2 = Level_2_Headings.all().ancestor(q.key()).filter("name2 =",heading2).get()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top