Home > Enterprise >  What fields can you call with request.user in Django?
What fields can you call with request.user in Django?

Time:03-25

Sorry for the dumb question but I can't figure it out.

I have a class based view for a form. I like to make some changes if the request.user is equal with something.

I used before in some other views request.user.profile.leader that gives me a boolean answer. Thats OK.

Now in this class based view I like to use a very similar stuff but with another model like this: request.user.trust.group but it gives me nothing. What am I doing wrong?

CodePudding user response:

If you haven't customized your user model, then profile will appear on it as a reverse descriptor of an one-to-one field on another model (by way of related_name having been set or inferred), i.e. you have something like

class Profile(Model):
    user = models.OneToOneField(User, related_name="profile")
    leader = models.BooleanField(...)

somewhere.

If you expect a trust field to be there, then you'd need something similar:

class Trust(Model):
    user = models.OneToOneField(User, related_name="trust")
    group = ...

On the other hand, if you do have an entirely custom user model, then those attributes could appear directly on it:

class CustomUser(AbstractBaseUser):
    profile = models.ForeignKey(Profile, ...)
    trust = models.ForeignKey(Trust, ...)
  • Related