golmschenk
golmschenk

Reputation: 12384

GAE - Model not saving while another model doing something similar does?

I'm having trouble getting a model to save (or be put()) correctly. The interesting part is that a model doing a very similar save before it works. Below are the relevant parts of the code. At the two logging points the first correctly returns the email of the user. However, the second one results in the error AttributeError: 'NoneType' object has no attribute 'c_user'. Obviously the setting and un-setting of the variables in this is not the correct way to do things, I've just added these to hunt down the problem to discover that the model isn't being saved. Any suggestions? Thank you much!

class Source(db.Model):
    current_user = db.UserProperty()

class SourceMember(db.Model):
    c_user = db.UserProperty()
    x_position = db.IntegerProperty()
    y_position = db.IntegerProperty()

...

user = users.get_current_user()

if user:
    source_key = self.request.get('g')
    if not source_key:
        source_key = user.user_id()
        source = Source(key_name = source_key,
                    current_user = user)
        source.put()
    else:
        source = Source.get_by_key_name(source_key)
    source = None
    source = Source.get_by_key_name(source_key)
    logging.warning(source.current_user)

    if source:
        sourceMember = SourceMember.get_by_key_name(user.user_id() + source_key)
        if not sourceMember:
            sourceMember = SourceMember(parent = source.key(),
                                        key_name = user.user_id() + source_key,
                                        c_user = user,
                                        x_position = None,
                                        y_position = None)
            sourceMember.put()
        sourceMember = None
        sourceMember = SourceMember.get_by_key_name(user.user_id() + source_key)
        logging.warning(sourceMember.c_user)

Upvotes: 0

Views: 43

Answers (1)

Greg
Greg

Reputation: 10360

When you create the SourceMember you're giving it a parent, but then when you get it, the parent is missing. Source doesn't have a parent, so getting it just from its id works.

Upvotes: 1

Related Questions