On 25 nov, 15:11, robos85 <prog...@gmail.com> wrote:
> Yes it looks that:
> from django.db import models
> from django.contrib.auth.models import User
>
> class UserProfile(models.Model):
>     user = models.ForeignKey(User, unique=True)
>     register_hash = models.CharField(max_length=32)
>
> And now I want to do something like this:
>     check = User.objects.all()
>
>     for x in check:
>         print x.userprofile.register_hash
>
> And I get :
> 'User' object has no attribute 'userprofile'
>
At least we're getting somewhere... Why didn't you start with this
above problem FWIW - would have saved everyone's time ;)

Is your UserProfile a "registration profile" (ie
http://bitbucket.org/ubernostrum/django-registration/) or is it a real
"user profile" (ie 
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users)
- or both, or None ?

In the second case, if you declare your UserProfile model in the
settings as speficied in the docs, you should be able to retrieve a
User's profile using the .get_profile() method.

In any case, since you 1/ used a ForeignKey - not a OneToOne field -
and 2/ didn't specified "userprofile" as the related name for the
relationship, the RelatedManager is accessible as "userprofile_set".
Also and FWIW, since it's many-to-one relationship, this
RelatedManager will work as documented for a standard ForeignKey.

Now given your first post, I think that what you really want here is,
given a email address (stored in the User model) and a reg_hash
(stored in the UserProfile model) check you do have the corresponding
records. The solution here is obviously not to do a whole scan of any
of the underlying tables (which would work but at a prohibitive cost),
but a direct joined query instead (as documented here :
http://docs.djangoproject.com/en/1.2/topics/db/queries/#lookups-that-span-relationships),
ie:

try:
    profile = UserProfile.objects.get(register_hash=reg_hash,
user__email=email)
except UserProfile.DoesNotExist:
    # handle the case here
else:
    # if you need to get at the user:
    user = profile.user


HTH

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