On Wed, May 21, 2008 at 2:00 PM, Jeremy Bornstein <[EMAIL PROTECTED]> wrote: > This brings up something I don't quite get: Django provides > user.get_profile(), but the ORM makes it just as easy to say > user.userprofile (assuming your profile model is called > "UserProfile"). Is there any reason to set up and use > user_get_profile() instead?
Yes, several. First off, if you have a simple model like so: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) website = models.URLField() Then on a User object, you actually would need to do: u = User.objects.get(username='bob') profile = u.userprofile_set.all()[0] The way reverse foreign key relations work will mean that 'u.userprofile' doesn't do anything (and, in fact, would raise an error). Second, the model might not be named "UserProfile" (and doesn't have to be named that), or might define a related_name, either of which changes the attribute created on the User model to refer to it. So blindly assuming that "userprofile" or, more correctly, "userprofile_set", would always work is dangerous (the same thing happens with custom managers -- you're not required to have a manager named "objects", so assuming it's there is begging for an AttributeError to come along and bite you). Using the APi Django exposes, through get_profile(), works around all of this by giving you a method whose name is always the same and which always returns one and only one profile object without you need to jump through related managers to get at it. -- "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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---