On Thu, 2009-07-30 at 12:33 -0700, zayatzz wrote:
> ... Hello!
> 
> I have a model (profile) which's only required field is its foreignkey
> - django.contrib.auth.User.
> 
> Following the example of forementioned model and its manager i created
> manager for the profile:
> 
> class ProfileManager(models.Manager):
>       def create_profile(self, username):
>               "Creates and saves a User with the given username, e-mail and
> password."
>               now = datetime.datetime.now()
>               profile = self.model(None, username)
>               profile.save()
>               return profile

It's very tricky to pass positional arguments to the __init__ method of
a model. Django does it itself when it creates models, because it knows
how the fields are structured, but it's very hard to get right and not
recommended in normal code. Instead, use keyword arguments. So you would
write something like this:

        user_instance = User.objects.get(username=username)
        profile = self.model(user = user_instance)
        profile.save()
        
The other change I've made here is to retrieve the correct User object,
since Django doesn't automatically know how to go from your "username"
string to a particular User instance.

> 
> and Profile model is like this :
> class Profile(models.Model):
>       user = models.ForeignKey(User, unique=True)
>         ......
>         several other stuff all have null=True
>         .......
>       objects = ProfileManager()
> 
> Now when i do this in a view:
>                       profile = Profile.objects.create_profile(request.user)
> 
> I get an error:
> 
> Exception Type:       TypeError
> Exception Value:      int() argument must be a string or a number, not
> 'User'
> 
> So why is this not working?

For the future, it is highly recommended to post the full traceback. On
the debug page you see in your web browser, there is a link that says
"cut-and-paste view". Click on that to get something that is suitable
for sticking in email. In this case, it's kind of possible to guess what
was causing the error (although I may have guessed poorly). At other
times, it isn't, so a little guidance from the traceback can often work
wonders.

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

Reply via email to