I reckon you could try the following:
- declare a function which does a 'select distinct' and returns the
queryset via the ORM
e.g.

def distinct_colors():
    results = FavoriteColor.objects.all().values("color").distinct()
    return results

then ...

class FavoriteThingsForm(forms.ModelForm):
    color = forms.ModelChoiceField(queryset=distinct_colors)

I think that you may have to explicitly declare all your form fields and
get rid of the class Meta declaration. Not verified, so head here for more
information:
https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield


On Mon, Aug 20, 2012 at 12:16 AM, Joseph Mutumi <jjmut...@gmail.com> wrote:

> Hello,
>
> I have a model that has a foreign key field that I want to use in a form
> as a select box.
> That particular field at times appears multiple times in the database. How
> do I make it
> only have distinct values?
>
> This is a snippet, drop down will have repeated values if same color is
> entered:
>
> class FavoriteColor(models.Model):
>     color = models.CharField(max_length=255)
>
>     def __unicode__(self):
>         return self.color
>
> class FavoriteThings(models.Model):
>     name = models.CharField(max_length=10)
>     color = models.ForeignKey(FavoriteColor)
>
> class FavoriteThingsForm(forms.ModelForm):
>     class Meta:
>         model = FavoriteThings
>
> Thank you
>
> --
> 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.
>



-- 
Regards,
Sithembewena Lloyd Dube

-- 
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