简体   繁体   English

如何将django中当前登录的用户配置文件添加到模型中?

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

How can I get current logged user profile in django, into models ? 如何将django中当前登录的用户配置文件导入模型?

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: 如果使用的是ModelForm,则有三个选择:

  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 将配置文件传递给save方法,并在调用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 在调用保存之前,在对象的实例上设置配置文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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