Django-在创建帐户类型的用户期间使用Profile模型扩展用户

时间:2019-03-07 07:52:38

标签: django

通过研究发现,为用户创建帐户类型的最佳方法是一对一模型,我将其命名为profile。尚不清楚的是我如何在创建用户的同时创建此内容,并且找不到明确的答案。我在Django中使用默认的用户模型。

我尝试了以下操作,但是它不起作用,并且尝试在get内使用post方法时显得很笨拙。

user = User.objects.create_user(first_name=first_name, last_name=last_name, password=password, username=email, email=email) 
user.save()
user = User.objects.get(pk=user_id)
user.profile.account_type = 'User'
user.save()

如果相关,我正在使用html格式的帐户类型单选按钮

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式简单地做到这一点:

this.CRUD.SelectionChanged += new System.EventHandler(this.CRUD_SelectionChanged);

或者您可以使用post save信号来保存个人资料。例如:

private void CRUD_SelectionChanged(object sender, EventArgs e)
{
    txtBoxID.Text = CRUD.CurrentRow.Cells[0].Value.ToString();
    txtBoxStates.Text = CRUD.CurrentRow.Cells[1].Value.ToString();
    txtBoxName.Text = CRUD.CurrentRow.Cells[2].Value.ToString();
    txtBoxAddress.Text = CRUD.CurrentRow.Cells[3].Value.ToString();
    txtBoxCenter.Text = CRUD.CurrentRow.Cells[4].Value.ToString();
    txtBoxCity.Text = CRUD.CurrentRow.Cells[5].Value.ToString();
}