Newbie Django filter uniques 2 many values

i am trying to filter the uniques from a list with this form:

class SpecForm(ModelForm):
    a = Doctors_list.objects.values_list('specialty', flat=True)
    unique = {z: i for i, z in a}
    qs = Doctors_list.objects.filter(id__in=unique.values())
    specialty = forms.ModelChoiceField(queryset=qs)

class Meta:
    model = Doctors_list

everything seems correct for me, but i get this error: too many values to unpack

any hints?


I think the correct statement should be this:

unique = {z: i for i in a}

Are you specifically trying to put those values into a dictionary? This will yield a list:

unique = [ i for i in a ]

If you go with this, you will have to remove the .values() in qs = Doctors_list.objects.filter(id__in=unique.values()) leaving it like this:

qs = Doctors_list.objects.filter(id__in=unique)

What's going on here is that with brackets in the first approach you're creating a dictionary with just one key and a list as a the value of that key. When you issue .values() you get a list with the dictionary's values. So it's pointless to use a dictionary.

With the second approach you get a list directly.

Hope it helps.

Need Your Help

Simple Bootstrap responsive header (navbar) drops on resize

css css3 twitter-bootstrap responsive-design

When you view the page with a wide browser window, it looks fine. If you resize the window to make it skinny, the header suddenly drops by 60px, which is equal to the body's padding-top. To make this

SQL - SQLDataAdapter Update causing primary key violation

c# sql primary-key insert-update sqldataadapter

I need to be able to change the primary keys in a table. The problem is, some of the keys will be changing to existing key values. E.g. record1.ID 3=>4 and record2.ID 4=>5. I need to keep these as