4

Model Code:

class Task(db.Model):
    complete = db.BooleanProperty(default=False)

HTML Code:

<input type="checkbox" name="complete" value="True" />

Database:

task = Task()
task.complete = self.request.get('complete')
task.put()

This returns an error:

BadValueError: Property complete must be a bool

How should this be done?

CT.
  • 285
  • 2
  • 13

2 Answers2

5

Since unchecked checkboxes are not sent as a parameter...

task.complete = self.request.get('complete') != ''
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 1
    Hey I guess I just have been refreshing the page too often, but I have seen all three of your edits. All three worked for me and I appreciate your help. Care to explain why this was better than the previously two working statements? Thanks. I'm new. – CT. Feb 26 '11 at 02:34
  • Mostly just it's the most straightforward. All 3 are perfectly fine options. `!= ''` would also be a viable option. – Amber Feb 26 '11 at 02:44
  • 'is not' tests for object identity. Python may make all empty strings the same object, but relying on that is probably a bad idea - I think using `!=` would be less prone to errors. :) – Nick Johnson Feb 28 '11 at 03:17
  • While I'm fairly sure every single serious Python interpreter out there will intern `''`, you're right that in theory it's a bad thing to rely on. Edited to `!=`. – Amber Feb 28 '11 at 05:09
0

You can use the type() funktion to check the tape of self.request.get('complete') I would suggest, that self.request.get('complete') returns 'True' but as a String so you should convert it to boolean. Here is a "list" of different methods

Community
  • 1
  • 1
V-Light
  • 3,065
  • 3
  • 25
  • 31