Distinct Values in ModelChoiceField

316 views
Skip to first unread message

Joseph Mutumi

unread,
Aug 19, 2012, 6:16:08 PM8/19/12
to django...@googlegroups.com
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

Sithembewena Lloyd Dube

unread,
Aug 19, 2012, 8:34:21 PM8/19/12
to django...@googlegroups.com
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



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



--
Regards,
Sithembewena Lloyd Dube

Melvyn Sopacua

unread,
Aug 20, 2012, 5:12:50 AM8/20/12
to django...@googlegroups.com
On 20-8-2012 0:16, Joseph Mutumi wrote:

> 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
You should invest in fixing the flaw in the design. Set the unique
attribute on the color field after you manually remove all duplicates
from the database.


--
Melvyn Sopacua

Sithembewena Lloyd Dube

unread,
Aug 21, 2012, 9:04:41 PM8/21/12
to django...@googlegroups.com
Hi Melvyn,

I found your response to the OP interesting, yet I cannot quite follow it. Even if he sets a unique attribute (presumably Boolean/ Checkbox?), he would still need a mechanism to filter the result set at runtime? Please expound?

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

Melvyn Sopacua

unread,
Aug 21, 2012, 9:18:24 PM8/21/12
to django...@googlegroups.com
On 22-8-2012 3:04, Sithembewena Lloyd Dube wrote:

> I found your response to the OP interesting, yet I cannot quite follow it.
> Even if he sets a unique attribute (presumably Boolean/ Checkbox?)

Nope, this unique attribute:
<https://docs.djangoproject.com/en/1.4//ref/models/fields/#unique>

By design, the foreign key will now not contain duplicates so distinct
is no longer necessary.
--
Melvyn Sopacua

Sithembewena Lloyd Dube

unread,
Aug 22, 2012, 6:44:43 AM8/22/12
to django...@googlegroups.com
@Melvyn, thanks - that makes sense.

--
Melvyn Sopacua

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

Joseph Mutumi

unread,
Aug 23, 2012, 3:23:29 PM8/23/12
to django...@googlegroups.com
@Melvyn Thank you. It actually worked out as you said. It needed a redesign.
Reply all
Reply to author
Forward
0 new messages