I am writing a Django program where Department/Position fields are associated with the User model. On the registration page, a person is required to enter first/last name, email, username, password, which are default User model fields, as well as their department and position.
我正在编写一个Django程序,其中部门/位置字段与用户模型相关联。在注册页面上,需要一个人输入first/last name, email, username, password,这是默认的用户模型字段,以及他们的部门和位置。
And then below are the two models I created.
下面是我创建的两个模型。
class Department(models.Model):
department_id = models.AutoField(primary_key=True)
department_name = models.CharField(max_length=100, unique=True)
is_active = models.BooleanField(default=True)
class Position(models.Model):
position_id = models.AutoField(primary_key=True)
position_name = models.CharField(max_length=100)
department = models.ForeignKey(Department)
user = models.OneToOneField(User, blank=True, related_name="position")
In views.py, the associated view goes like this:
在视图。py,关联视图是这样的:
def sign_up_in(request):
global user
post = request.POST
if post['email'] is u'' or post['password'] is u'':
msg = 'Please check sign-up input forms'
return render_to_response('none.html', {'msg': msg})
else:
if not user_exists(post['email']):
user = create_user(username=post['email'], email=post['email'], password=post['password'])
user.first_name=post['first']
user.last_name=post['last']
**user.position.position_name=post['position']**
user.department=post['department']
user.is_staff = True
user.save()
msg = 'Sign Up OK ' + user.first_name + ' ' + user.last_name
else:
msg = 'Existing User'
return render_to_response('login.html', {'msg': msg})
When I added the boldfaced line in views.py above, I started getting the error "No exception supplied". What should I change in the models, and views? Also, in this line of code, user = create_user(username=post['email'], email=post['email'], password=post['password']), how should I express the foreign key relation?
当我在视图中添加粗体线时。在上面,我开始得到“没有提供异常”的错误。我应该在模型和视图中更改什么?另外,在这一行代码中,user = create_user(username=post['email'], email=post['email'], password=post['password']),我该如何表达外键关系?
1 个解决方案
#1
1
The simple answer is that user.position
does not yet exist.
简单的答案就是用户。职位还不存在。
Let's break it down to get it to work.
让我们把它分解一下,让它工作。
def sign_up_in(request):
...
post = request.POST
... # Your if block
# 1. Create your user.
user = User.objects.create_user(post['email'], post['email'], post['password'])
user.first_name = post['first']
user.last_name = post['last']
user.is_staff = True
user.save()
# 2. Create/get your department.
dept, created = Department.objects.get_or_create(department_name=post['department'])
# 3. Create your position
position = Position.objects.create(department=dept, user=user, position=post['position'])
...
Note that the User.objects.create_user()
and Foo.objects.create()
automatically save the object, so you only need to save again if you add more data (as in the user above).
注意,user .objects.create_user()和Foo.objects.create()会自动保存对象,因此,如果您添加更多的数据(如上面的用户所示),您只需要再次保存。
As a side note, although this will solve the problem you are having, I would advise you to scrap this particular view and use a Form class to handle this. Form classes will allow you handle a lot of this stuff in much easier ways and provides some much needed validation methods. You can checkout the relevant documentation here: https://docs.djangoproject.com/en/1.6/topics/forms/
作为补充说明,尽管这将解决您遇到的问题,但我建议您废弃这个特定的视图,并使用表单类来处理它。表单类将允许您以更简单的方式处理这些内容,并提供一些非常需要的验证方法。您可以在这里签出相关的文档:https://docs.djangoproject.com/en/1.6/topics/forms/。
#1
1
The simple answer is that user.position
does not yet exist.
简单的答案就是用户。职位还不存在。
Let's break it down to get it to work.
让我们把它分解一下,让它工作。
def sign_up_in(request):
...
post = request.POST
... # Your if block
# 1. Create your user.
user = User.objects.create_user(post['email'], post['email'], post['password'])
user.first_name = post['first']
user.last_name = post['last']
user.is_staff = True
user.save()
# 2. Create/get your department.
dept, created = Department.objects.get_or_create(department_name=post['department'])
# 3. Create your position
position = Position.objects.create(department=dept, user=user, position=post['position'])
...
Note that the User.objects.create_user()
and Foo.objects.create()
automatically save the object, so you only need to save again if you add more data (as in the user above).
注意,user .objects.create_user()和Foo.objects.create()会自动保存对象,因此,如果您添加更多的数据(如上面的用户所示),您只需要再次保存。
As a side note, although this will solve the problem you are having, I would advise you to scrap this particular view and use a Form class to handle this. Form classes will allow you handle a lot of this stuff in much easier ways and provides some much needed validation methods. You can checkout the relevant documentation here: https://docs.djangoproject.com/en/1.6/topics/forms/
作为补充说明,尽管这将解决您遇到的问题,但我建议您废弃这个特定的视图,并使用表单类来处理它。表单类将允许您以更简单的方式处理这些内容,并提供一些非常需要的验证方法。您可以在这里签出相关的文档:https://docs.djangoproject.com/en/1.6/topics/forms/。