I am getting a TypeError that my Django ChoiceField's choices are unhashable Type: 'list'

29 views Asked by At

I'm new to Django.

I use formset and form via TabularInline. First, I'll show you my forms.py code.

DISCONN_BLANK = ((None, '---disconnect---'),)
CONN_BLANK = ((None, '---connect---'),)


class MyFormSet(BaseInlineFormSet):

    def get_form_kwargs(self, index):
        kwargs = super(MyFormSet, self).get_form_kwargs(index)
        kwargs.update({'parent': self.instance})
        return kwargs


class MyForm(select2_modelform(Conninfo)):

    class Meta:
        model = Conninfo
        fields = ['mgr_id', ]

    parent_value = None

    def __init__(self, *args, **kwargs):
        instance = kwargs.pop('parent')
        if instance:
            self.parent_value = instance
            print('MyFrom __init__ parent_value {}'.format(self.parent_value))
        super(MyForm, self).__init__(*args, **kwargs)

        conn_queryset = tuple(Conninfo.objects.filter(client_id=self.parent_value).values('mgr_id').values_list('mgr_id', 'mgr_id'))
        disconn_queryset = tuple(Devicemanager.objects.exclude(mgr_id__in=Conninfo.objects.all().values('mgr_id')).values_list('mgr_id', 'mgr_id'))

        mgr_id_widget = select2_modelform(Conninfo)().fields['mgr_id'].widget

        choices = DISCONN_BLANK + tuple(disconn_queryset) + CONN_BLANK + tuple(conn_queryset)
        print(choices)
        print(type(choices))
        self.fields['mgr_id'] = forms.ChoiceField(choices=choices, widget=mgr_id_widget)

Does it seem very complicated..? The point is the conn_queryset and disconn_queryset and choices parts.

The type of choices is tuple, and when I try to print ((None, '---disconnect---'), (('id1', 'id1'), ('id2', 'id2'), ... ('id10', 'id10'), ( None, '---connect---'), ('id14', 'id14'), ('id15', 'id15')) It comes in the form of a tuple.

But what about the results? TypeError at unhashable type: 'list'

where is the list? Isn't list {}? I really don't know.

And the strange thing is that if you take the last code out of the init function from conn_queryset, it will be executed. Of course, it doesn't give me the results I want.

I really don't know what to do. I couldn't solve it for two days.

I would be grateful if you could let me know.

I checked that everything is a tuple. I also confirmed that there is no list. I also studied TypeError.

0

There are 0 answers