Search code examples
pythongoogle-app-enginereferenceproperty

Google app engine ReferenceProperty view won't post/save


Here are my models and views. I'm new to App Engine and I am trying to create a new language set with a relationship to profiletitle. I've read through a ton of posts/docs and can't find a good example of functional view code.

models.py

class AddProfile(db.Model):
    profiletitle = db.StringProperty(required=True)


class AddLang(db.Model):
    profile = db.ReferenceProperty(AddProfile, required=True)
    language = db.StringListProperty()
    date = db.DateTimeProperty(auto_now_add=True)

views.py

class CreateLang(BaseHandler):

    def post(self):
        n = AddLang(
        profile = self.request.get('profile'),
        language = self.request.get('language').split(', '))
        n.put()
        return webapp2.redirect('/')

    def get(self):
        self.render_template('create.html', {})

updated view.py - this now stores data in the datastore

class CreateLang(BaseHandler):

    def post(self):
        n = AddLang(
        profile = AddProfile.all().filter('profiletitle', self.request.get('profile')).get(),
        language = self.request.get('language').split(', '))
        n.put()
        return webapp2.redirect('/')

    def get(self):
        self.render_template('create.html', {})

create.html

<!DOCTYPE html>

<html>
<body>
<form action="" method="post">
  <div class="field-wrapper">

    <div class="field-label">
      <label for="id_profile">Profile</label>:
    </div>

    <div class="field-field">
      <input type="text" name="profile" id="id_profile" />

    </div>

 </div>

  <div class="field-wrapper">

    <div class="field-label">
      <label for="id_language">Language</label>:
    </div>

    <div class="field-field">
      <input type="text" name="language" id="id_language" />

    </div>

</div>
  <br>
  <input type="submit" value=submit />
</form>

When I attempt to post/save the data I receive the following error. I believe this is caused because I need to use the get to call the specific id of the profiletitle selected in my template.

Note: I am not using a form py, I have a simple html template. Let me know if you have any questions or need to see the template. Thanks for reading/helping.

     ![datastore snip]:(http://imm.io/1hTVN)

 Error Message
     if value is not None and not value.has_key():

 AttributeError: 'unicode' object has no attribute 'has_key'

Solution

  • It would have been helpful to show exactly what you're sending in the POST. But assuming you're sending a profile title, you do need to get it:

    profile = AddProfile.all().filter('profiletitle', self.request.get('profile')).get()
    

    Now you can use that profile object to create your AddLang:

    lang = AddLang(profile=profile, language=language)
    lang.put()