Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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 must be an int. But request.user is an object. Why queries are the same for object and user id?

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

Thanks in advance

like image 602
TheNone Avatar asked Jan 10 '12 13:01

TheNone


1 Answers

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

If this wasn't the behaviour, either passing the pk would be required, or an error, which would merely be annoying.

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

like image 141
Marcin Avatar answered Oct 20 '22 00:10

Marcin