views:

521

answers:

1

Im using appengine and the appenginepatch (so my issue could be related to that)

I have set up a model with a property that has several choices but when trying to display on a form or via admin interface I am getting an error:

Property mode is 'o'; must be one of (('s', 'Single'), ('m', 'Multi'), ('o', 'Ordered'))

This is my code:

MODES = (
  ('s', 'Single'),
  ('m', 'Multi'),
  ('o', 'Ordered')
)

class X(search.SearchableModel):
    mode = models.StringProperty( default='s', choices=MODES )

if I set it to use Integers (as below) the admin form (and my own ModelForm) shows each option for the property as the whole tuplet so that when I select and try to save I get the error that I'm not saving an Integer value

MODES = (
  (0, 'Single'),
  (1, 'Multi'),
  (2, 'Ordered')
)

class X(search.SearchableModel):
    mode = models.IntegerProperty( default=0, choices=MODES )

Is there something special I have to do?

+2  A: 

It looks like this is an issue in Django/appengine support. It's documented here on the google-app-engine-django bug tracker, but it's closed as "wontfix" there. It is also documented here on the googleappengine bug tracker and is closed as invalid.

According to the docs, the appengine choices parameter works different than the Django one. You do not appear to be able to do what you want without creating a custom widget. According to Guido's comment closing the googleappengine ticket,

I realize that this may cause problems when you're trying to create a form from the model, but the solution is to override the form field using a custom widget and passing the list of desired choices to the widget. (There's an example of this in Rietveld, in codereview/views.py, class SettingForm.)

Rick Copeland
Ah great thanks, I couldn't find those references. The workaround should be good enough. Cheers