{"action":"created","issue":{"url":"https://api.github.com/repos/blreams/collegefootballpick10/issues/4","labels_url":"https://api.github.com/repos/blreams/collegefootballpick10/issues/4/labels{/name}","comments_url":"https://api.github.com/repos/blreams/collegefootballpick10/issues/4/comments","events_url":"https://api.github.com/repos/blreams/collegefootballpick10/issues/4/events","html_url":"https://github.com/blreams/collegefootballpick10/issues/4","id":85582931,"number":4,"title":"User Model Clarification","user":{"login":"jbholden","id":1541768,"avatar_url":"https://avatars.githubusercontent.com/u/1541768?v=3","gravatar_id":"","url":"https://api.github.com/users/jbholden","html_url":"https://github.com/jbholden","followers_url":"https://api.github.com/users/jbholden/followers","following_url":"https://api.github.com/users/jbholden/following{/other_user}","gists_url":"https://api.github.com/users/jbholden/gists{/gist_id}","starred_url":"https://api.github.com/users/jbholden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jbholden/subscriptions","organizations_url":"https://api.github.com/users/jbholden/orgs","repos_url":"https://api.github.com/users/jbholden/repos","events_url":"https://api.github.com/users/jbholden/events{/privacy}","received_events_url":"https://api.github.com/users/jbholden/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-05T17:05:38Z","updated_at":"2015-06-26T01:38:51Z","closed_at":"2015-06-26T01:38:51Z","body":"I have been trying to use the rest-framework model (see http://www.django-rest-framework.org) to create an API to allow Users to be added.  However, I have not yet figured out how to get the serializer to work with the User model and UserProfile model.\r\n\r\nCan you explain the following code from models.py?  I understand the UserProfile class, but I am not sure what the last statement is doing.  I think it is adding a profile attribute to the User Model.\r\n\r\n```Python\r\nclass UserProfile(models.Model):\r\n    user = models.OneToOneField(User)\r\n    company = models.CharField(max_length=50, blank=True)\r\n\r\n# what does this do?\r\nUser.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])\r\n```\r\n\r\nGiven this code, how would you go about creating a User?  Specifically, I am struggling with setting the profile field.  I think method 2 might be the right method, but I cannot get the user.profile.company method to set a value other than ''\r\n```\r\n# create a user method 1?\r\nuser = add_user(username, email, firstname, last name)\r\nuser_profile = UserProfile.objects.create(user=user,company='Intel')\r\nuser.profile = user_profile\r\nuser.save()\r\n\r\n# create a user method 2?\r\nuser = add_user(username, email, firstname, last name)\r\nuser.profile.company = 'Intel'\r\nuser.save()\r\n\r\n# something else?\r\n```"},"comment":{"url":"https://api.github.com/repos/blreams/collegefootballpick10/issues/comments/115453406","html_url":"https://github.com/blreams/collegefootballpick10/issues/4#issuecomment-115453406","issue_url":"https://api.github.com/repos/blreams/collegefootballpick10/issues/4","id":115453406,"user":{"login":"blreams","id":5880937,"avatar_url":"https://avatars.githubusercontent.com/u/5880937?v=3","gravatar_id":"","url":"https://api.github.com/users/blreams","html_url":"https://github.com/blreams","followers_url":"https://api.github.com/users/blreams/followers","following_url":"https://api.github.com/users/blreams/following{/other_user}","gists_url":"https://api.github.com/users/blreams/gists{/gist_id}","starred_url":"https://api.github.com/users/blreams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/blreams/subscriptions","organizations_url":"https://api.github.com/users/blreams/orgs","repos_url":"https://api.github.com/users/blreams/repos","events_url":"https://api.github.com/users/blreams/events{/privacy}","received_events_url":"https://api.github.com/users/blreams/received_events","type":"User","site_admin":false},"created_at":"2015-06-26T01:38:51Z","updated_at":"2015-06-26T01:38:51Z","body":"After playing around with this in the shell a bit, I figured out that your latter method was close, but lacked one thing. Here is the correct way to do this:\r\n```\r\n# create a user method 3\r\nuser = add_user(username, email, firstname, lastname)\r\nuser.userprofile.company = 'Intel'\r\nuser.userprofile.save()   # This was the thing you were missing\r\nuser.save()\r\n```\r\n\r\nAlso, the lambda function way of referencing the profile was something I got from an older version of django, that isn't needed anymore. I cleaned up a few things related to UserProfile along with adding it to the admin (you can see and set company now when you view a particular user). Now that this is working, we can change UserProfile to suit our needs."}}