2

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.

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:

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?

user2857014
  • 507
  • 3
  • 10
  • 22

1 Answers1

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

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/

patsweet
  • 1,548
  • 10
  • 12