I am learning how to use Google App Engine / Python. (webapp)
I am coming from Java (could be my problem!), where if I put objects in a Swing list box, it would invoke their toString() method for display purposes. When I selected one of them it would return the object and not just the representation of it generated by toString().
I have a Person model that holds a persons details:
class Person(db.Model):
'''represents a single person'''
first = db.StringProperty()
last = db.StringProperty()
address = db.StringProperty()
city = db.StringProperty()
region = db.StringProperty()
postal = db.StringProperty()
country = db.StringProperty()
phone = db.StringProperty()
cell = db.StringProperty()
email = db.StringProperty()
comment = db.StringProperty(multiline=True)
And a Reservation model that stores information about the room, and stores what Person belongs with the Reservation:
class Reservation(db.Model):
'''represents a single reservation'''
room = db.StringProperty()
start_day = db.IntegerProperty()
start_month = db.IntegerProperty()
start_year = db.IntegerProperty()
end_day = db.IntegerProperty()
end_month = db.IntegerProperty()
end_year = db.IntegerProperty()
percent_discount = db.IntegerProperty()
comment = db.StringProperty(multiline=True)
client = #what would go here?
To get the reservation info from the user, and put the reservation into the database, I have an HTML form with various fields including rooms, arrival date, departure date, etc. One of them includes a “client list” that gets the list of Persons that are already in the database:
<select size="5" name="client_list">
{% for person in clients %}
<option>{{ person.first|escape }} {{ person.last|escape }}</option>
{% endfor %}
</select>
This works to display the names, but then I don’t know how to store the selected “Person” or client into the Reservation in the following code:
class Bookings(webapp.RequestHandler):
'''Handles all of the bookings'''
def post(self):
'''adds a new booking into the db'''
reservation = models.Reservation()
reservation.room = self.request.get('room')
reservation.start_day = int(self.request.get('start_day'))
reservation.start_month = util.month_to_int(self.request.get('start_month'))
reservation.start_year = int(self.request.get('start_year'))
reservation.end_day = int(self.request.get('end_day'))
reservation.end_month = util.month_to_int(self.request.get('end_month'))
reservation.end_year = int(self.request.get('end_year'))
reservation.percent_discount = int(self.request.get('percent_discount'))
reservation.comment = self.request.get('comment')
#This would get the clients first + last name, but not the object Person
#reservation.client = self.request.get('client_list')
reservation.put()
self.redirect('/bookings')
I started to look at Keys (specifically db.Key.from_path), and how every Person made in GAE generates a unique ID, but then I still don’t know how to get that unique ID assigned to a person from the list displayed in the HTML if all that is displayed is the persons name.
This seems like something that is possible, and possibly simple but after two days of searching I have come up with nothing. Let me know if I should provide any more information, this is my first post here at Stack Overflow! Thanks for any advise.
According to the documentation:
So you have to pass client’s
key, not full name. Fix your template code:Point that
<option>element has its ownvalueattribute. And note that why I changed<select>element’s name fromcleint_listtoclientis that it cannot select multiple items, but only one item.Then, retrieve the client instance by its key. According to the documentation:
So insert the following code in your application: