Re: Distinct Values in ModelChoiceField

2012-08-23 Thread Joseph Mutumi
@Melvyn Thank you. It actually worked out as you said. It needed a redesign.

On Wed, Aug 22, 2012 at 1:44 PM, Sithembewena Lloyd Dube
wrote:

> @Melvyn, thanks - that makes sense.
>
>
> On Wed, Aug 22, 2012 at 3:18 AM, Melvyn Sopacua wrote:
>
>> 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:
>> 
>>
>> By design, the foreign key will now not contain duplicates so distinct
>> is no longer necessary.
>> --
>> 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-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.
>

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



Re: Distinct Values in ModelChoiceField

2012-08-22 Thread Sithembewena Lloyd Dube
@Melvyn, thanks - that makes sense.

On Wed, Aug 22, 2012 at 3:18 AM, Melvyn Sopacua wrote:

> 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:
> 
>
> By design, the foreign key will now not contain duplicates so distinct
> is no longer necessary.
> --
> 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-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.



Re: Distinct Values in ModelChoiceField

2012-08-21 Thread Melvyn Sopacua
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:


By design, the foreign key will now not contain duplicates so distinct
is no longer necessary.
-- 
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-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.



Re: Distinct Values in ModelChoiceField

2012-08-21 Thread Sithembewena Lloyd Dube
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?

On Mon, Aug 20, 2012 at 11:12 AM, Melvyn Sopacua wrote:

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



Re: Distinct Values in ModelChoiceField

2012-08-20 Thread Melvyn Sopacua
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

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



Re: Distinct Values in ModelChoiceField

2012-08-19 Thread Sithembewena Lloyd Dube
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  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.



Distinct Values in ModelChoiceField

2012-08-19 Thread Joseph Mutumi
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.