On Tue, Jul 20, 2010 at 11:38 AM, 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?

The "current user", that is, the authenticated user, is typically available
in a view as request.user (if you are not in a view there is no "current"
user).  There is middleware that adds (a property that accesses on
demand and caches) the user associated with the session.  If you are
some function or method that doesn't have access to request, your
best bet is to modify things so that request is passed in, or that it has
been hung on an object that is passed in (you can say, in the view,
roommate.request = request if roommate doesn't have a request
field and then call a method on roommate that accesses self.request -
just don't expect it to persist from request to request, or for other
code that looks up the same roommate from the database,
though caching could make this look as though it is working).

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