简体   繁体   中英

how does one get the profile from a user in DJango

I have code that is like the following for people logging in:

if request.method == 'POST':

username = request.POST.get('username')
password = request.POST.get('password')

user = authenticate(username=username, password=password)

if user:
    # Check it the account is active
    if user.is_active:

        # Log the user in.
        login(request, user)

I have extended the User to create a UserProfile. It contains other information. Ex: address, city, state, etc.

How would one extract the User profile from this situation? For Java under Eclipse, if I typed in "user", I would see all of the valid methods that would apply to the object "user".

It does not appear that PyCharm has such a feature.

With that being said, how can one find the profile information associated with a user?

TIA

Below is the Profile Model Code:

class UserProfileInfo (models.Model):
    class Meta:

        db_table = 'mstrauthdjprofile'
        db_tablespace = 'PSAPUSR1001'

    user = models.OneToOneField(User)

    restrole = models.BigIntegerField(blank=True, null=True, default=0)

    profilepic = models.ImageField(upload_to='profile_pics', blank=True)

    lastpgprocno = models.BigIntegerField(blank=True, null=True, default=-1)
    currentpgprocno = models.BigIntegerField(blank=True, null=True, default=-1)
    nextpgprocno = models.BigIntegerField(blank=True, null=True, default=1)

    reclocktype = models.BigIntegerField(blank=True, null=True, default=0)
    reclockid = models.BigIntegerField(blank=True, null=True, default=0)

    def __str__(self):
        return self.user.username

OneToOneField s are always accessible from their counterparts.

user = authenticate(username=username, password=password)
user.userprofileinfo

user = User.objects.first()
user.userprofileinfo

user_profile_info = UserProfileInfo.objects.first()
user_profile_info.user

You can use a debugger like pdb by inserting a breakpoint in your code (I believe PyCharm should have an automated way to handle this):

import pdb; pdb.set_trace()

Which will allow you to interact with the current scope. You can view what attributes a object has with __dict__

user.__dict__
{'username': 'user', 'id': 1, ...}

Also consider a more powerful/interactive debugger like IPython or bpython, which have auto-complete built in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM