简体   繁体   中英

How can I get current logged user profile in django, into models?

How can I get current logged user profile in django, into models ?

I have this code:

def save(self, *args, **kwargs):
        profile = User.get_profile()
        self.empresa_id =   profile.idempresa
        self.empresa_id =  profile.id_comerx3c
        super(Cliente, self).save(*args, **kwargs)

but doesn`t work..

If you're using a ModelForm, you have three choices:

  1. Set the profile when you instantiate the form
  2. Create the object instance in a view, but not commit it, then set the profile property and commit the save
  3. Pass the profile to the save method, and set the property before calling super

If you're just dealing with a Model, you have two choices:

  1. Pass the profile in when you instantiate the object
  2. Set the profile on an instance of your object before you call save

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