Same result for pk = request.user and pk = request.user.id in django

class MyUser(User): job = ... city = .... 

MyUser.objects.get(pk=request.user) and MyUser.objects.get(pk=request.user.id) give me the same result. From doc pk should be int. But request.user is an object. Why are the requests the same for the object identifier and user?

edit: I know request.user is an object, but I want to know why the results are the same.

Thanks in advance

+6
source share
1 answer

This is by design - if you pass the model object as a parameter to the request, it is the same as passing its primary key.

If this was not a behavior, either passing pk or an error that is just annoying is required.

Thanks to the rebus for this source link: https://code.djangoproject.com/browser/django/trunk/django/db/models/fields/related.py#L175

+9
source

Source: https://habr.com/ru/post/905617/


All Articles