开发者

google app engine (python) confusing class 'object has no attribute' error

I have a 2 classes.

One looks like this:

class Feed(db.Model):
    bid = db.StringProperty()
    title = db.StringProperty()
    url = db.StringProperty()
    datecreated = db.DateProperty(auto_now_add=True)
    voice = db.StringProperty()
    lastchecked = db.DateProperty(auto_now=True)
    language = db.StringProperty()
    active = db.BooleanProperty()
    posts = db.ListProperty(db.Key)

The other looks like this:

class Post(db.Model):
    title = db.StringProperty()
    postdate = db.DateTimeProperty()
    author = db.StringProperty()
    body = db.TextProperty()
    link = db.LinkProperty()
    hasmp3 = db.BooleanProperty()
    mp3location = db.StringProperty()
    processed = db.BooleanProperty()
    voice = db.StringProperty()
    length = db.FloatProperty()
    inprocess = db.BooleanProperty()
    haspict = db.BooleanProperty()
    pictures = db.ListProperty(db.Key)

I am trying to update a Feed by appending a Post like this:

blogid = cgi.escape(self.request.get('bid'))
postid = cgi.escape(self.request.get('pid'))

blog = Feed.get_by_key_name(blogid)
post = Post.get_by_key_name(postid)

if post.key() not in blog.posts:
    blog.posts.append(post.key())
    blog.put()

When I attempt to 'put' the blog with the post information, python doesn't like it and tells me

AttributeError: 'Feed' object has no attribute 'posts'

The reason I am so confused is because I have nearly the exact logic in adding a 'Feed' to a 'UserClass' where the 'UserClass' has 'Feeds' designated as a ListProperty in the same way that a 'Feed' has a 'Post' designated as a ListProperty.

This is an example of what I mean:

class UserClass(db.Model):
    synccode = db.StringProperty()
    firstname = db.StringProperty()
    lastname = db.StringProperty()
    address = db.StringProperty()
    address2 = db.StringProperty()
    city = db.StringProperty()
    state = db.StringProperty()
    zipcode = db.StringProperty()
    emailaddress = db.StringProperty()
    password = db.StringProperty()
    mobile = db.StringProperty()
    is开发者_开发知识库_authenticated = db.BooleanProperty()
    groupid = db.StringProperty() 
    mobilesynched = db.BooleanProperty()
    devicemodel = db.StringProperty()
    isatmoscast = db.BooleanProperty() 
    registerdate = db.DateProperty(auto_now_add=True) 
    pack = db.StringProperty()
    personalcontact = db.BooleanProperty()
    lastlogin = db.DateProperty(auto_now_add=True)
    isactive = db.BooleanProperty()
    feeds = db.ListProperty(db.Key)

This works fine:

if blog.key() not in user.feeds:
    user.feeds.append(blog.key())
    user.put()

This throws the error:

if post.key() not in blog.posts:
    blog.posts.append(post.key())
    blog.put()

Any help would be appreciated.


I've pasted your models in a "hello world" main.py; then running it on the local SDK, I enter on the interactive console:

import main as m

p = m.Post()
p.put()
f = m.Feed()
f.put()

if p.key() not in f.posts:
    f.posts.append(p.key())
    f.put()

print f.posts

and I see, just as expected, in the results window to the right:

[datastore_types.Key.from_path(u'Post', 7, _app=u'helow')]

In other words, it seems impossible to reproduce the problem you report in a simple case.

Please make a showstheproblem.py, as simple as possible (i.e., no code that's not indispensable to reproduce the problem!), that shows the problem, and edit your question to add that code (minus the models, we've already seen those and of course the vast majority of their fields is utterly irrelevant to this problem).

This is always the right way to go about reporting a suspected bug or getting help with it; more often than not, you'll find that in the process of whittling down a copy of your code in order to get the "minimum possible reproduction of the problem"... the problem disappears in a way that shows you what was wrong with your code in the first place -- so you won't actually even have to ask for help about, or report, the non-existent bug in the library!-)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜