Alexxio
Alexxio

Reputation: 1101

Access User fields using UserProfile object

I want to access User model fields using UserProfile object. I have a 'role' field in Userprofile,

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    role = models.CharField(max_length=30) 

Each user has a role, 'supervisor' or 'admin' now I want to get the user objects whose role=supervisor. I want to be able to access the username of those users with role supervisor which is in User model customized. I cant get the query right. Help please.

Upvotes: 2

Views: 120

Answers (1)

mVChr
mVChr

Reputation: 50205

supervisor_usernames = [up.user.username for up in UserProfile.objects.filter(
    role='supervisor')]

Upvotes: 1

Related Questions