Reputation: 461
I want to be able to use ModelChoiceField in my form for my model in django admin. But while the default widget Select works when I customize and add
class GalleryModelForm(forms.ModelForm):
image = forms.ModelChoiceField(queryset=models.Photo.objects.all(),
widget=forms.SelectMultiple, empty_label=None)
class Meta:
model = models.Gallery
I get
int() argument must be a string or a number, not 'list'
from traceback:
/app/.heroku/python/lib/python2.7/site-packages/django/db/models/fields/__init__.py in get_prep_value
def get_prep_value(self, value):
if value is None:
return None
return int(value)
Is there any way to pass string or number to get_prep_value
or to override it?
Upvotes: 0
Views: 909
Reputation: 2442
You should use ModelMultipleChoiceField class instead of using a ModelChoiceField with setting a SelectMultiple widget I think.
And thus it would give you the following:
class GalleryModelForm(forms.ModelForm):
image = forms.ModelMultipleChoiceField(queryset=models.Photo.objects.all(), empty_label=None)
class Meta:
model = models.Gallery
And then, just take care if the form is left empty, Django 1.5 will return an empty queryset, while older versions will return an empty list (ugly).
Upvotes: 2