fh_bash
fh_bash

Reputation: 1795

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

Upvotes: 0

Views: 276

Answers (1)

Brandon Taylor
Brandon Taylor

Reputation: 34553

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

Upvotes: 1

Related Questions