Hi,

If user is a FK to the User model, then simply self.user.first_name
should work.

http://www.djangoproject.com/documentation/models/many_to_one/

Have you set the user property on the object you're trying to save?
What error do you get?

Btw what exactly are you trying to achieve with this line (I simply
didn't understand)?
User.get_profile = lambda self: Profile.objects.get_or_create(user=self)[0]

Nuno

On Tue, Jul 20, 2010 at 4:38 PM, reduxdj <patricklemi...@gmail.com> wrote:
> OK, from my example, the model is called Roommate, so I am not
> duplicating any User model data there... So all of the information
> in that model has nothing to do the main user's email or other
> properties.
>
> Thanks for helping, I still don't have an answer from the above. How
> do
> I access the current user's email and name?
>
> On Jul 20, 10:33 am, Bill Freeman <ke1g...@gmail.com> wrote:
>> You don't say what model this is.  Since it doesn't exactly match
>> django.contrib.auth.models, I'm going to guess that you may not
>> be aware that the User model includes first_name, last_name,
>> and email fields of its own.  Having duplicated those fields, are
>> you perhaps becoming confused about where to find things?
>> The typical registration apps usually place this data in the User
>> instance, so if you're looking for it in your instance, you would
>> have had to arrange to copy it everytime something changes it
>> in either model.
>>
>> Duplicating data in one-to-one models (if what I'm looking at is
>> your profile model) isn't likely to win "best practice" accolades.
>>
>> Bill
>>
>> On Tue, Jul 20, 2010 at 10:08 AM, reduxdj <patricklemi...@gmail.com> wrote:
>> > HI,
>>
>> > So I tried for an hour or so to access  the current user's first name,
>> > so I can send out a personalized invitation email. That reads, "Hi
>> > Sam, Your friend Charlie wants you to sign up here..." I understand
>> > how to access the user info from context, but how do I do it from
>> > inside one of my models with a user foreign key?
>>
>> > Now, what's funny is I don't know how to access the current user's
>> > first name.  I tried the following.
>>
>> > Thanks for the help!
>>
>> > Here's some code below.
>>
>> >    user =  models.ForeignKey(User)
>> >    first_name = models.CharField(max_length=200)
>> >    last_name = models.CharField(max_length=200)
>> >    email = models.CharField(max_length=200)
>> >    inv_date  = models.DateTimeField(auto_now_add=True)
>> >    is_registered = models.BooleanField()
>>
>> >    #def __unicode__(self):
>> >    #    return self.user.username
>>
>> >    def save(self, *args, **kwargs):
>>
>> >        if send_mail:
>> >            User.get_profile = lambda self:
>> > Profile.objects.get_or_create(user=self)[0]
>> >            current_site = Site.objects.get_current()
>> >            #subject =
>> > render_to_string('roommate_invitation_subject.txt')
>> >            # Email subject *must not* contain newlines
>> >            subject = "Hello..."
>> >            subject = ''.join(subject.splitlines())
>> >            message = render_to_string('roommate_invitation_email.txt',
>> > {'invitee':self.first_name,'inviter': self.user.first_name,'site':
>> > current_site })
>> >            send_mail('Invitation to xxx', message, 'invite_...@xxx',
>> > [str(self.email)])
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups 
>> > "Django users" group.
>> > To post to this group, send email to django-us...@googlegroups.com.
>> > To unsubscribe from this group, send email to 
>> > django-users+unsubscr...@googlegroups.com.
>> > For more options, visit this group 
>> > athttp://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@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.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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