Re: Problem saving a User object to a profile

2008-12-19 Thread DragonSlayre

Actually, it seems a bit redundant doesn't it?  I see there is a
get_profile, but nothing to set it anyway.  I guess it's derived
information from the database Yay! such simple solutions, I'm so
happy to be able to register.

On Dec 20, 9:42 am, DragonSlayre  wrote:
> You are gods - thank you.  I did try something like this before, but
> being so noob at the technology, didn't assign the form to a new
> variable.
>
> Will i have to assign the profile in the user object as well?  I
> assume I will.
>
> On Dec 20, 9:38 am, Daniel Roseman 
> wrote:
>
> > On Dec 19, 8:28 pm, nbv4  wrote:> On Dec 19, 3:15 pm, 
> > DragonSlayre  wrote:
>
> > > > profile_form.user = user;
> > > > profile_form.save()
>
> > > The problem is that you can't assign fields in forms directly like
> > > this. The only way to input data into a form is when you initially
> > > create the form object ala:
>
> > > profile_form = ProfileForm(request.POST)
>
> > > If theres another way I'd love to know too, because I ran into this
> > > problem earlier and my only solution was to manually create an hidden
> > > textbox with the user_id.
>
> > 
>
> > The easiest way is to do it like this:
> > new_profile = profile_form.save(commit=False)
> > new_profile.user = request.user
> > new_profile.save()
> > --
> > DR.
--~--~-~--~~~---~--~~
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: Problem saving a User object to a profile

2008-12-19 Thread DragonSlayre

You are gods - thank you.  I did try something like this before, but
being so noob at the technology, didn't assign the form to a new
variable.

Will i have to assign the profile in the user object as well?  I
assume I will.

On Dec 20, 9:38 am, Daniel Roseman 
wrote:
> On Dec 19, 8:28 pm, nbv4  wrote:> On Dec 19, 3:15 pm, 
> DragonSlayre  wrote:
>
> > > profile_form.user = user;
> > > profile_form.save()
>
> > The problem is that you can't assign fields in forms directly like
> > this. The only way to input data into a form is when you initially
> > create the form object ala:
>
> > profile_form = ProfileForm(request.POST)
>
> > If theres another way I'd love to know too, because I ran into this
> > problem earlier and my only solution was to manually create an hidden
> > textbox with the user_id.
>
> 
>
> The easiest way is to do it like this:
> new_profile = profile_form.save(commit=False)
> new_profile.user = request.user
> new_profile.save()
> --
> DR.
--~--~-~--~~~---~--~~
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: Problem saving a User object to a profile

2008-12-19 Thread nbv4



On Dec 19, 3:15 pm, DragonSlayre  wrote:
> profile_form.user = user;
> profile_form.save()

The problem is that you can't assign fields in forms directly like
this. The only way to input data into a form is when you initially
create the form object ala:

profile_form = ProfileForm(request.POST)

If theres another way I'd love to know too, because I ran into this
problem earlier and my only solution was to manually create an hidden
textbox with the user_id.

also, make sure if the user already has a profile in the database,
that you create a bound form:

profile_form = ProfileForm(request.POST,
instance=request.user.get_profile())


--~--~-~--~~~---~--~~
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: Problem saving a User object to a profile

2008-12-19 Thread Daniel Roseman

On Dec 19, 8:28 pm, nbv4  wrote:
> On Dec 19, 3:15 pm, DragonSlayre  wrote:
>
> > profile_form.user = user;
> > profile_form.save()
>
> The problem is that you can't assign fields in forms directly like
> this. The only way to input data into a form is when you initially
> create the form object ala:
>
> profile_form = ProfileForm(request.POST)
>
> If theres another way I'd love to know too, because I ran into this
> problem earlier and my only solution was to manually create an hidden
> textbox with the user_id.


The easiest way is to do it like this:
new_profile = profile_form.save(commit=False)
new_profile.user = request.user
new_profile.save()
--
DR.

--~--~-~--~~~---~--~~
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: Problem saving a User object to a profile

2008-12-19 Thread Adi Sieker

Hi,

On 19.12.2008, at 21:15, DragonSlayre wrote:

>
> I've got a profile model which has a django.contrib.auth.models.User
> as a foreign key.
>
> I am trying to assign the User object to my profile model after I've
> checked that the user is valid and that the profile contents are also
> valid.
>
> Once I've saved my user object, I then call
>
> profile_form.user = user;
> profile_form.save()
>
I assume that you are using ModelForm to create your form.
If so, check the documentation [1] and you'll know that the save  
method returns the model instance.
So you should be able todo:

profile = profile_form.save()
profile.user = user
profile.save()

that'll cause 2 database hits, but should work fine.

adi

--~--~-~--~~~---~--~~
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: Problem saving a User object to a profile

2008-12-19 Thread DragonSlayre

Ok - I'm digging deeper, and I realise that my form doesn't have a
user, as it's excluded from the form using exclude = ('user').

I'll carry on looking...
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Problem saving a User object to a profile

2008-12-19 Thread DragonSlayre

I've got a profile model which has a django.contrib.auth.models.User
as a foreign key.

I am trying to assign the User object to my profile model after I've
checked that the user is valid and that the profile contents are also
valid.

Once I've saved my user object, I then call

profile_form.user = user;
profile_form.save()

In my database, the user and profile have been created, but the
profile's foreign key to the user hasn't been created.

I'm guessing that I'm not calling the right thing on the profile_form,
but am not sure what I should be calling?

Thanks for your help :)


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