Tuesday, September 28, 2010

Re: User.get_profile() not working

On Tue, Sep 28, 2010 at 2:45 PM, adj7388 <adj7388@gmail.com> wrote:
> Django newbie issue. Just trying to understand. I'm setting up a
> simple UserProfile class to link to User (as described in several
> places in documentation). Here's what I have --- a simple example of
> storing the user's website in a profile
>
> #In myapp/models.py
> class UserProfile(models.Model):
>    def __init__(self, website='http://www.default.com'):
>        super(UserProfile, self).__init__()
>        self.website = website
>    user = models.ForeignKey(User, unique=True,
> related_name="user_profile") <-- note related_name...see below
>    website = models.URLField()


Why did you do that? and not:

#In myapp/models.py
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True,

related_name="user_profile") <-- note related_name...see below
website = models.URLField(default='http://www.default.com')


>
> TypeError: __init__() takes at most 2 arguments (4 given)
>

That's cause __init__() of a model actually takes more arguments than
you allow. Redefining __init__ goes something like:

> class UserProfile(models.Model):
> def __init__(self, *args, **kwargs):
website = kwargs.get('website', "default_url")
> super(UserProfile, self).__init__(*args, **kwargs)
> self.website = website

Note, I said _something like_ there could be more ways to do it.

Cheers,
--
Ale.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment