如何将django中当前登录的用户配置文件导入模型?

如何将django中当前登录的用户配置文件导入模型?,django,django-models,Django,Django Models,如何将django中当前登录的用户配置文件导入模型 我有以下代码: 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) 但

如何将django中当前登录的用户配置文件导入模型

我有以下代码:

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)

但是不起作用。

如果您使用的是ModelForm,则有三种选择:

  • 在实例化表单时设置配置文件
  • 在视图中创建对象实例,但不提交,然后设置概要文件属性和 提交保存
  • 将配置文件传递给save方法,并在 呼叫超级
  • 如果您只是在处理模型,您有两个选择:

  • 实例化对象时传入配置文件
  • 在调用save之前,在对象实例上设置配置文件

  • 您需要当前用户的实际实例您必须手动将该用户实例传递给您的保存函数。我如何才能做到这一点?我是django的新手……我使用的是:def editaclient(request,pessoa_id=None):if pessoa_id:cliente=cliente.objects.get(id=pessoa_id)else:cliente=None if request.method='POST':formPessoa=ClienteForm(request.POST,instance=cliente)if formPessoa.is有效():cliente=formPessoa.save()如果cliente:return HttpResponse('cliente salvo com successo!')否则:formPessoa=ClienteForm(instance=cliente)return render_to_response('cliente_novo.html',locals(),context_instance=RequestContext(request)),我得到了答案。。。if request.method='POST':formPessoa=ClienteForm(request.POST,instance=cliente)if formPessoa.is\u valid():profile=request.user.get\u profile()cliente=formPessoa.save(commit=False)cliente.empresa_id=profile.idempresa cliente.idrepresente=profile.id\u comerx3c cliente=formPessoa.save()如果cliente:return HttpResponse('cliente salvo com successo!')或者:formPessoa=ClienteForm(instance=cliente)谢谢大家。。