0

Is there any reason this doesn't work? / Any way to make it work? (without defining another choice set)

class Foo(models.Model):
    BAR_CHOICES = ((str(x), str(x)) for x in range(5))

    bar = models.CharField(max_length = 1, choices = BAR_CHOICES)
    barbar = models.CharField(max_length = 1, choices = BAR_CHOICES)

class FooForm(forms.ModelForm):
    class Meta:
        model = Foo

class FooAdmin(admin.ModelAdmin):
    pass

admin.site.register(Foo, FooAdmin)

Then if you go to the admin page or make a ModelForm, the choices only show up for the first field

Like so:

>> foo_form = FooForm()
>> print foo_form
<tr><th><label for="id_bar">Bar:</label></th><td><select name="bar" id="id_bar">
<option value="" selected="selected">---------</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select></td></tr>
<tr><th><label for="id_barbar">Barbar:</label></th><td><select name="barbar" id="id_barbar">
<option value="" selected="selected">---------</option>
</select></td></tr>

enter image description here

edited to show FooForm, FooAdmin, and Admin img

Ben
  • 6,986
  • 6
  • 44
  • 71

1 Answers1

2

You've defined Foo.BAR_CHOICES via a genex. Use a LC instead so that it can be iterated over multiple times.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Ignacio you are the man. I think you've replied to everyone of my questions so far. Would you explain that a bit more though? Just tried to search genex/LC and I couldn't find anything. Thank you – Ben Jun 04 '12 at 07:48
  • [Generator Expressions vs. List Comprehension](http://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehension) – Ignacio Vazquez-Abrams Jun 04 '12 at 07:50