I want to retrieve the value of the hidden id field from a form in a formset in a view. When I use
pk = form.cleaned_data['id']
I receive the model rather than the id. Instead I have to use
pk = form.cleaned_data['id'].pk
Why is that? What would be the best way to do this?
That is the best way to handle it.
Yep, I agree with @KenWhitesell.
Presumably id
is a ModelChoiceField
, so it gives you a model instance.
To do it any other way, you’d need to use a plain ChoiceField
and generate the choices from the a QuerySet, using the raw id value, which the choice field would then give you back.
That’s just to replace the work that ModelChoiceField
is already doing for you, for no benefit, and quite likely edge-cases missed.