0
votes

I have an entity called Author. Below is the ndb class:

class Author(ndb.Model):
    name = ndb.StringProperty()
    website = ndb.StringProperty()
    bio = ndb.StringProperty()
    profile_image_url = ndb.StringProperty()
    slug = ndb.ComputedProperty(lambda self: make_slug(self.name))

I am creating an author object like this

author = Author(id=pk, **author_dict)
author.put()

There is no error when creating an author object for the first time.

I am retrieving an author object like this

author = Author.get_by_id(pk)

when I try to access the author bio field I get an AttributeError

author.bio = bio_text

The error comes when accessing a field on a existing author object. Error does not come on all fields.

The console is showing all the columns. The bio column is present in the datastore console for author entity.

I deleted all the author entities and made new entries. Still I am getting AttributeError.

I have several entities in datastore but I am getting this error only for Author entity.

Error statement:

File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", 
line 2990, in _set_attributes
prop = getattr(cls, name)  # Raises AttributeError for unknown properties.
AttributeError: type object 'Author' has no attribute 'bio'

CLOSED:

I had a class called Author in another module. This class was being used by NDB somehow to create the author entity and was causing this behaviour.

1
Please can you provide an minimal reproducible example. I can't reproduce this problem with the example code that you have provided. - snakecharmerb
Hi, I have added some more information. I can add the surrounding code but it is unrelated to the error. - zaphod100.10
You can only get this error if "author" is a class, not an instance, and is a class that does not have a bio attribute. So it seems that you are overwriting author somewhere in your code. - snakecharmerb
You could try printing or logging type(author) and author.__name__ just before the line that's failing, to see what author actually is. - snakecharmerb
I tried your suggestion and no matter what fields I have in the Author class, type(Author) is printing Author<author_id=StringProperty('author_id'), name=StringProperty('name'), profile_image_url=StringProperty('profile_image_url')> - zaphod100.10

1 Answers

0
votes

I had a class called Author in another module. This class was being used as a structured property in another entity class.

Somehow this class was being used by NDB to create the Author entity. I renamed this class and the error was gone.

Now I am using different class names for all ndb entities and structured properties.