문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top