On 5/27/07, ZZorba <[EMAIL PROTECTED]> wrote:
>
> Hi
> I'm in trouble while trying to write simple codes for modifying user
> profiles.
>
> My purpose is to display current user's prior profile on modifying
> templates.
> For example, if user already have '[EMAIL PROTECTED]' as his email,
> then '[EMAIL PROTECTED]' should be initially displayed input box.
>
> However, I could do that via 'request.user.get_profile().email', yet,
> there are many other fields.
> Thus, it's silly to write down all like,
>
> "
> ...
> request.user.get_profile().email
> request.user.get_profile().email_backup
> request.user.get_profile().brother
> request.user.get_profile().sister
> ...
> "
>
> So, i'm looking forward to finding out alternative ways like,
>
> "
> profile_fields = "email email_backup brother sister".split()
> for key in profile_fields:
>     request.user.get_profile().__getattr__(key)
> "

Sounds like you want:

for field in request.user.get_profile()._meta.fields:
    print getattr(request.user.get_profile(), field.name)

Some additional notes regarding your solution, and other suggestions:
- __getattr__ is an internal method, and shouldn't be invoked
directly. Like all double underscore methods, it indicates that the
method is part of a protocol - you should be using the protocol,
rather than invoking the method directly.

- The eval() technique suggested in another reply may work, but isn't
really a good practice. eval() exists to make interactive prompts
possible, not as a general programming technique. There is no
protection on what eval() will do, so if an attacker can modify the
string that is being eval()'d, they have complete access to your
system. This is obviously very bad practice on a public-facing
interface, like a web site.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to