Re: auth: get_profile(): create if it does not exist.
Where does this code go? I want to create a user profile for every user created. On Dec 18 2008, 2:37 am, "James Bennett"wrote: > On Wed, Dec 17, 2008 at 5:21 PM, Malcolm Tredinnick > > wrote: > > This would be the "standard" solution. I thought James Bennett's > > django-profiles app did this, but apparently I'm mistaken (it would be > > slightly duplicated work if it did, in any case). > > django-profiles kicks you to a profile-creation view if you try to > edit your profile and don't have one already. > > django-registration, on the other hand, supports passing a callback to > create an initial profile for a newly-registered user (in other words, > a function which just knows how to create an instance of your profile > model with all blank/default values). > > -- > "Bureaucrat Conrad, you are technically correct -- the best kind of correct." --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: auth: get_profile(): create if it does not exist.
On Wed, Dec 17, 2008 at 5:21 PM, Malcolm Tredinnickwrote: > This would be the "standard" solution. I thought James Bennett's > django-profiles app did this, but apparently I'm mistaken (it would be > slightly duplicated work if it did, in any case). django-profiles kicks you to a profile-creation view if you try to edit your profile and don't have one already. django-registration, on the other hand, supports passing a callback to create an initial profile for a newly-registered user (in other words, a function which just knows how to create an instance of your profile model with all blank/default values). -- "Bureaucrat Conrad, you are technically correct -- the best kind of correct." --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: auth: get_profile(): create if it does not exist.
> ## > try: > m = SomeModel.objects.get(pk=request.user) > except: > m = SomeModel() > ## > > Attention, catch-all ("except" without an exception class) is evil. There are a lot of exception that could happen. You could use: try: except SomeModel.DoesNotExist: ... or m, created = SomeModel.objects.get_or_create(pk=request.user) To my original question: Yes, I will use a signal handler to create the profiles. Thank you for your answers (Milian Andric). Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: auth: get_profile(): create if it does not exist.
On Wed, 2008-12-17 at 08:25 -0600, Milan Andric wrote: > On Wed, Dec 17, 2008 at 4:00 AM, Thomas Guettlerwrote: > > > > Hi, > > > > The method user.get_profile() fails, if the user has no profile. During > > my custom login I > > check if the user has a profile and create it if needed. > > > > But sometimes this fails: A new user gets created, but before his first > > login someone else > > tries to access the not yet created profile. > > > > Since all fields of my profile model have default values, it could be > > created it on the fly. > > > > Since I don't want to run a modified django, I will use my own > > get_profile method. > > > > Does some know this problem? How do you solve this? > > > > Thomas > > > > How about setting up a signal on user creation that also creates a profile? This would be the "standard" solution. I thought James Bennett's django-profiles app did this, but apparently I'm mistaken (it would be slightly duplicated work if it did, in any case). Register a handle for post_save on the User model, and, in the handler, if created=True, create the associated user profile. Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: auth: get_profile(): create if it does not exist.
actually don't user super(self.__class__, self) http://loveandtheft.org/2008/09/03/how-super-should-be-used-when-calling-a-parents-method/ use super(MyManager, self) On 17 Dec, 18:15, 7timesTomwrote: > On 17 Dec, 16:08, Darthmahon wrote: > > > Why not create your own function then and just put the try or except > > code in that? That way it'll only be one line. > > Yup. Using model.Manager i.e.: > > class MyManager(models.Manager): > def get_or_blank(self, pk_val): > try: > m = super(self.__class__, self).get_query_set().get > (pk=pk_val) > except self.model.DoesNotExist: > m = self.model() > return m > > then you'd need, in each model def: > objects = MyManager() > > Now you can do: > s = SomeModel.objects.get_or_blank(35) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: auth: get_profile(): create if it does not exist.
On 17 Dec, 16:08, Darthmahonwrote: > Why not create your own function then and just put the try or except > code in that? That way it'll only be one line. Yup. Using model.Manager i.e.: class MyManager(models.Manager): def get_or_blank(self, pk_val): try: m = super(self.__class__, self).get_query_set().get (pk=pk_val) except self.model.DoesNotExist: m = self.model() return m then you'd need, in each model def: objects = MyManager() Now you can do: s = SomeModel.objects.get_or_blank(35) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: auth: get_profile(): create if it does not exist.
Why not create your own function then and just put the try or except code in that? That way it'll only be one line. On Dec 17, 3:19 pm, nbv4wrote: > On Dec 17, 5:00 am, Thomas Guettler wrote: > > > > > Hi, > > > The method user.get_profile() fails, if the user has no profile. During > > my custom login I > > check if the user has a profile and create it if needed. > > > But sometimes this fails: A new user gets created, but before his first > > login someone else > > tries to access the not yet created profile. > > > Since all fields of my profile model have default values, it could be > > created it on the fly. > > > Since I don't want to run a modified django, I will use my own > > get_profile method. > > > Does some know this problem? How do you solve this? > > I'm having the same problem. I have about 4 models that are 1to1 > linked to a user, much like how the get_profile() thing works. Every > time I want to access that model, I have to do this: > > ## > try: > m = SomeModel.objects.get(pk=request.user) > except: > m = SomeModel() > ## > > Which gets annoying to have to do every single time. I wish there was > some way to do something like: > > ## > m = SomeModel.objects.get_or_blank(pk=request.user) > ## > > Yeah, sure you can have it create a row in the database every time a > new user registers, but why hit the database when all you ever need is > the default values? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: auth: get_profile(): create if it does not exist.
On Dec 17, 5:00 am, Thomas Guettlerwrote: > Hi, > > The method user.get_profile() fails, if the user has no profile. During > my custom login I > check if the user has a profile and create it if needed. > > But sometimes this fails: A new user gets created, but before his first > login someone else > tries to access the not yet created profile. > > Since all fields of my profile model have default values, it could be > created it on the fly. > > Since I don't want to run a modified django, I will use my own > get_profile method. > > Does some know this problem? How do you solve this? I'm having the same problem. I have about 4 models that are 1to1 linked to a user, much like how the get_profile() thing works. Every time I want to access that model, I have to do this: ## try: m = SomeModel.objects.get(pk=request.user) except: m = SomeModel() ## Which gets annoying to have to do every single time. I wish there was some way to do something like: ## m = SomeModel.objects.get_or_blank(pk=request.user) ## Yeah, sure you can have it create a row in the database every time a new user registers, but why hit the database when all you ever need is the default values? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: auth: get_profile(): create if it does not exist.
On Wed, Dec 17, 2008 at 4:00 AM, Thomas Guettlerwrote: > > Hi, > > The method user.get_profile() fails, if the user has no profile. During > my custom login I > check if the user has a profile and create it if needed. > > But sometimes this fails: A new user gets created, but before his first > login someone else > tries to access the not yet created profile. > > Since all fields of my profile model have default values, it could be > created it on the fly. > > Since I don't want to run a modified django, I will use my own > get_profile method. > > Does some know this problem? How do you solve this? > > Thomas > How about setting up a signal on user creation that also creates a profile? -- Milan --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: auth: get_profile(): create if it does not exist.
Can you not do this: if request.user.is_authenticated(): # Get profile user.get_profile() else: # Create profile On Dec 17, 10:00 am, Thomas Guettlerwrote: > Hi, > > The method user.get_profile() fails, if the user has no profile. During > my custom login I > check if the user has a profile and create it if needed. > > But sometimes this fails: A new user gets created, but before his first > login someone else > tries to access the not yet created profile. > > Since all fields of my profile model have default values, it could be > created it on the fly. > > Since I don't want to run a modified django, I will use my own > get_profile method. > > Does some know this problem? How do you solve this? > > Thomas > > -- > Thomas Guettler,http://www.thomas-guettler.de/ > E-Mail: guettli (*) thomas-guettler + de --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---