Ron
Ron

Reputation: 23516

TypeError in view: Field expected a number but got SimpleLazyObject

I upgraded to django v3.1 and suddenly getting this error:

Field 'id' expected a number but got <SimpleLazyObject: '23'>.

The failing line of code is this:

my_obj = get_object_or_404(MyModel, pk=kwargs.get('pk'))

Any ideas what I might be doing wrong?

Thanks!

Upvotes: 3

Views: 1100

Answers (1)

Ron
Ron

Reputation: 23516

After some googling around I found this post by Aaron O. Ellis.

The trick is not to use the kwargs directly but from the view class with self:

my_obj = get_object_or_404(MyModel, pk=self.kwargs.get('pk'))

Hope this helps anybody else as well.

Upvotes: 4

Related Questions