Tuesday, September 28, 2010

Re: User.get_profile() not working

Using *args and **kwargs might work

then maybe

self.website = kwargs.get('website', 'default.com')

On Sep 28, 1:45 pm, adj7388 <adj7...@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()
>
> #In myproject/settings.py
> AUTH_PROFILE_MODULE = "myapp.UserProfile"
>
> Now I create a empty db
> run: python2.6 manage.py syncdb
> All tables look good in postgres.
>
> Now in the python shell:
>
> Python 2.6.5 (r265:79063, May 12 2010, 10:28:19)
>
> >>> from django.contrib.auth.models import User
> >>> from myapp.models import UserProfile
> >>> u = User(username='testuser', password='shhhh')
> >>> u.save()
> >>> up = UserProfile(website='www.overridethesite.com')
> >>> up.user=u
> >>> up.save()
>
> So far so good
> Over in Postgres, everything looks fine:
> mydb=> select au.id as id, au.username, up.id as upid, up.user_id as
> fk_userid, up.website from auth_user au join myapp_userprofile up on
> (au.id=up.user_id);
>    id | username | upid | fk_userid |         website
> ------+----------+------+-----------+-------------------------
>     2 | testuser |    1 |         2 |www.overridethesite.com
> (1 row)
>
> Now back in the python shell, trying to use the profile from the user
> obj:
>
> >>> u.user_profile.values()  <--- works fine using the related_name I set in the UserProfile class
>
> [{'website': u'www.overridethesite.com', 'user_id': 2, 'id': 1}]
>
> ### But get_profile() fails:
>
> >>> myprofile = u.get_profile()
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
>   File "/home/myusername/webapps/django_boosters/lib/python2.6/django/
> contrib/auth/models.py", line 370, in get_profile
>     self._profile_cache =
> model._default_manager.using(self._state.db).get(user__id__exact=self.id)
>   File "/home/myusername/webapps/django_boosters/lib/python2.6/django/
> db/models/query.py", line 336, in get
>     num = len(clone)
>   File "/home/myusername/webapps/django_boosters/lib/python2.6/django/
> db/models/query.py", line 81, in __len__
>     self._result_cache = list(self.iterator())
>   File "/home/myusername/webapps/django_boosters/lib/python2.6/django/
> db/models/query.py", line 282, in iterator
>     obj = self.model(*row[index_start:aggregate_start])
> TypeError: __init__() takes at most 2 arguments (4 given)
>
> Can anyone tell me what's going on, or what I'm doing wrong? Thanks in
> advance.

--
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