title: Django create user profile at the time of registration from submission
body: <p>I am working on a Role Based Access Control system on django.where at the signup/register people will be designated to a 'department' and will be assigned a 'role' Hence I created a custom user model</p>
  
  <p>models.py </p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import User
  from django.db.models.signals import post_save
  from django.dispatch import receiver
  
  
   class Profile(models.Model):
  
   DEPARTMENT_CHOICES = (
      ('Production', 'Production'),
      ('Marketing', 'Marketing'),
      ('IT', 'IT'),
      ('HR', 'HR'),
      ('Accounts', 'Accounts'),
  )
  ROLE_CHOICES = (
      ('Manager', 'Manager'),
      ('Team Lead', 'Team Lead'),
      ('Member', 'Member'),
  )
  
  user = models.OneToOneField(
      User,)
  department = models.CharField(
      max_length=50, choices=DEPARTMENT_CHOICES, null=True)
  role = models.CharField(max_length=50, choices=ROLE_CHOICES, null=True)
  
  def __unicode__(self):
      return unicode(self.user.first_name + ' ' + self.user.last_name)
  
  def __str__(self):
      return self.user.first_name + ' ' + self.user.last_name
  
  
  def create_profile(sender, **kwargs):
      if kwargs['created']:
      user_profile = Profile.objects.create(user=kwargs['instance'])
      pass
  
  post_save.connect(create_profile, sender=User)
  </code></pre>
  
  <p>Then i created a form like</p>
  
  <p>forms.py</p>
  
  <pre><code>from django import forms
  from django.contrib.auth.forms import UserCreationForm
  from django.contrib.auth.models import User
  from .models import Profile
  
  
  class SignUpForm(UserCreationForm):
  DEPARTMENT_CHOICES = (
      ('Production', 'Production'),
      ('Marketing', 'Marketing'),
      ('IT', 'IT'),
      ('HR', 'HR'),
      ('Accounts', 'Accounts'),
  )
  ROLE_CHOICES = (
      ('Manager', 'Manager'),
      ('Team Lead', 'Team Lead'),
      ('Member', 'Member'),
  )
  first_name = forms.CharField(
      max_length=30, required=True, help_text='Optional.')
  last_name = forms.CharField(
      max_length=30, required=True, help_text='Optional.')
  email = forms.EmailField(
      max_length=254, required=False)
  department = forms.ChoiceField(choices=DEPARTMENT_CHOICES, )
  role = forms.ChoiceField(choices=ROLE_CHOICES, )
  
  class Meta:
      model = User
      fields = ('first_name', 'last_name', 'username',
                'email', 'password1', 'password2', 'department', 'role')
  </code></pre>
  
  <p>The my views</p>
  
  <pre><code>from django.shortcuts import render, redirect, render_to_response
  from django.contrib.auth.decorators import login_required
  from django.contrib.auth import login, authenticate
  from .forms import SignUpForm
  from .models import Profile
  from django.shortcuts import get_object_or_404
  from django.http import Http404
  from django.contrib.auth.models import User
  from django.template import RequestContext
  
  def register(request):
  if request.user.is_authenticated:
      if request.user.is_superuser or request.user.role == 'Manager':
          if request.method == 'POST':
              form = SignUpForm(request.POST)
              if form.is_valid():
                  user = form.save()
                  userprofile = Profile.objects.update(
                      user=user, department=form.cleaned_data['department'])
                  userprofile = Profile.objects.update(
                      user=user, role=form.cleaned_data['role'])
          else:
              form = SignUpForm()
  
          c = {
              'form': form,
          }
  
          return render(request, 'registration/register.html', c)
      else:
          raise Http404
  else:
      raise Http404
  </code></pre>
  
  <p>my register.html</p>
  
  <pre><code>{% extends 'layouts/base.html' %} {% load widget_tweaks %}{% block additionalbottom %}
  &lt;script&gt;
  var allParas = document.getElementsByTagName('div');
  //filter by class name if desired...
  for (var i = 0; i &lt; allParas.length; i++) {
      if (allParas[i].getElementsByTagName('*').length == 0) {
          allParas[i].style.display = 'none';
      }
  }
  &lt;/script&gt;
  {% endblock %} {% block content %}
  &lt;div class="login-page"&gt;
      &lt;div class="form"&gt;
          &lt;h2&gt;User Registration&lt;/h2&gt;
          &lt;form class="login-form" method="post"&gt;
              {% csrf_token %}
              &lt;div class="row"&gt;
                  {% endblock %} {% for field in form %}
                  &lt;div class="col-md-6"&gt;
                      {% if field.name == "first_name" or field.name == "last_name" %} {% render_field field class="form-control" placeholder=field.label.capitalize %} {% endif %}
                  &lt;/div&gt;
                  &lt;div class="col-md-6"&gt;
                      {% if field.name == "username" or field.name == "email" %} {% render_field field class="form-control" placeholder=field.label.capitalize %} {% endif %}
                  &lt;/div&gt;
                  &lt;div class="col-md-6 form-group"&gt;
                      {% if field.name == "department" or field.name == "role" %} {% render_field field class="form-control" placeholder=field.label.capitalize %} {% endif %}
              &lt;/div&gt;
              &lt;div class="col-md-12"&gt;
                  {% if field.name == "password1" %} {% render_field field class="form-control" placeholder=field.label.capitalize %} {% endif %}
              &lt;/div&gt;
              &lt;div class="col-md-12"&gt;
                  {% if field.name == "password2" %} {% render_field field class="form-control" placeholder=field.label.capitalize %} {% endif %}
              &lt;/div&gt;
              {% endfor %}
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;button type="submit"&gt;Register&lt;/button&gt;
          &lt;/div&gt;
      &lt;/form&gt;
      {% if form.errors %} {% for field in form %} {% for error in field.errors %}
      &lt;div class="alert alert-danger"&gt;
          &lt;strong&gt;{{ error|escape }}&lt;/strong&gt;
      &lt;/div&gt;
      {% endfor %} {% endfor %} {% for error in form.non_field_errors %}
      &lt;div class="alert alert-danger"&gt;
          &lt;strong&gt;{{ error|escape }}&lt;/strong&gt;
      &lt;/div&gt;
      {% endfor %} {% endif %}
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>now when ever i try to submit my form it says </p>
  
  <blockquote>
    <p>IntegrityError at /register/
    column user_id is not unique</p>
  </blockquote>
  
  <p>i tried so many solutions from google and SO nothing seems to solve the problem can anyone tell me how to fix it or why it cannot be done if it cannot be done what is the work around</p>