On Thursday, February 24, 2011 03:53:03 am galago wrote:
> I need to make a form, which have 1 select and 1 text input. Select must be
> taken from database.
> model looks like this:
> class Province(models.Model):
>     name = models.CharField(max_length=30)
>     slug = models.SlugField(max_length=30)
> 
>     def __unicode__(self):
>         return self.name
> 
> It's rows to this are added only by admin, but all users can see it in
> forms.
> I want to make a ModelForm from that. I made something like this:
> class ProvinceForm(ModelForm):
>     class Meta:
>         CHOICES = Province.objects.all()
> 
>         model = Province
>         fields = ('name',)
>         widgets = {
>             'name': Select(choices=CHOICES),
>         }
> 
> but it doesn't work. The select tag is not displayed in html. What did I
> wrong?

I haven't done this one in a while, but you need to make the CHOICES a tuple 
or a list[1], not a queryset (unless there has been changes in this specific 
area).

something like this:

def make_choices():
        choices = []
        for item in Province.objects.all()
                choices.append((item.name, item.name))
   return choices
        
# your form stuff
  ...
        widgets = { 'name': Select(choices=make_choices()), }

The first value is the value stored, the second one is the human readable form 
in the tuple.

Mike

[1] http://docs.djangoproject.com/en/1.2/ref/models/fields/#field-choices


-- 
Banacek's Eighteenth Polish Proverb:
        The hippo has no sting, but the wise man would rather be sat upon
        by the bee.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to