{"action":"opened","issue":{"url":"https://api.github.com/repos/Volentix/venue-server/issues/161","repository_url":"https://api.github.com/repos/Volentix/venue-server","labels_url":"https://api.github.com/repos/Volentix/venue-server/issues/161/labels{/name}","comments_url":"https://api.github.com/repos/Volentix/venue-server/issues/161/comments","events_url":"https://api.github.com/repos/Volentix/venue-server/issues/161/events","html_url":"https://github.com/Volentix/venue-server/issues/161","id":347646790,"node_id":"MDU6SXNzdWUzNDc2NDY3OTA=","number":161,"title":"Do not use .exists()","user":{"login":"alexdashkov","id":6633228,"node_id":"MDQ6VXNlcjY2MzMyMjg=","avatar_url":"https://avatars2.githubusercontent.com/u/6633228?v=4","gravatar_id":"","url":"https://api.github.com/users/alexdashkov","html_url":"https://github.com/alexdashkov","followers_url":"https://api.github.com/users/alexdashkov/followers","following_url":"https://api.github.com/users/alexdashkov/following{/other_user}","gists_url":"https://api.github.com/users/alexdashkov/gists{/gist_id}","starred_url":"https://api.github.com/users/alexdashkov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexdashkov/subscriptions","organizations_url":"https://api.github.com/users/alexdashkov/orgs","repos_url":"https://api.github.com/users/alexdashkov/repos","events_url":"https://api.github.com/users/alexdashkov/events{/privacy}","received_events_url":"https://api.github.com/users/alexdashkov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-08-04T20:39:20Z","updated_at":"2018-08-04T20:39:20Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"I see a lot of examples on the code when you use `.exists` method. \r\n\r\nFor example:\r\n\r\n```python\r\nuser_check = User.objects.filter(\r\n    email__iexact=data.get('email').strip()\r\n)\r\nif user_check.exists():\r\n    try:\r\n        user_profile = UserProfile.objects.get(\r\n            user=user_check.last()\r\n        )\r\n```\r\n\r\nPlease, do not use this method if you plan to get data in the future. When you run it you execute 2 database queries instead of only one. If user object doesn't exist you will get an empty DB query that cost nothing for the DB, but if a user exists you will get only query instead of two. For example this part of a code you can rewrite like this: \r\n\r\n```python\r\nuser_check = User.objects.filter(\r\n    email__iexact=data.get('email').strip()\r\n).last() #  None if there is no user\r\n# and here all the verification you have \r\n```\r\nAlso in this part you can see that you execute two query for the User model and one for user profile, but you can make only one like this: \r\n```sql \r\nSELECT \"venue_userprofile\".\"email_confirmed\"\r\nFROM \"auth_user\"\r\n       LEFT JOIN \"venue_userprofile\" ON (\"auth_user\".\"id\" = \"venue_userprofile\".\"user_id\")\r\nWHERE UPPER(\"auth_user\".\"email\" :: TEXT) = UPPER(SOME email)\r\n```\r\nUsing ORM query like this one: \r\n```python\r\nemail_confirmed = User.objects.filter(\r\n     email__iexact=data.get('email').strip()\r\n).values('profiles__email_confirmed').last()\r\nresponse['email_exists'] = email_confirmed['profiles__email_confirmed'] if email_confirmed else False\r\n```"}}
{"actor":{"display_login":"alexdashkov"}}