Skip to content Skip to sidebar Skip to footer

Django-cannot Assign "u'joan Manel'": "despesa.nomtreballador" Must Be A "treballador" Instance

I have been working on a project in which I have to point out the expenses that the workers of a company have. For this I have created two models, workers and expenses, in which ex

Solution 1:

You are getting this error because you are passing a text string to be used as the nomTreballador foreign key, while you should be passing a treballador instance.

It looks like you're trying to restrict the available choices to a set of distinct trebelladors by using a forms.ChoiceField, but a better way to do this with a ModelForm is to change the queryset attribute of the nomTreballador field. You do this in the form's init method:

self.fields['nomTreballador'].queryset = treballador.objects.all().distinct()

Also you should check the clean methods you've implemented because not all of them map to an existing field.

Post a Comment for "Django-cannot Assign "u'joan Manel'": "despesa.nomtreballador" Must Be A "treballador" Instance"