title: Django Duplicate entry '2' for key 'user_id'
body: <p>Im trying to do a registration page in Django 1.4.</p>
  
  <p>These are my files:</p>
  
  <p>view.py</p>
  
  <pre><code>    mensaje = ""
          if request.method == 'POST':
                  form = RegistrationForm(request.POST)
                  if form.is_valid():
                          usr = User.objects.create_user(username = form.cleaned_data['username'], email = form.cleaned_data['mail'], password = form.cleaned_data['pa$
  
                          cli = cliente(user=usr, nombre=form.cleaned_data['nombre'], apellidos = form.cleaned_data['apellidos'], empresa = form.cleaned_data['empresa$
                          mensaje = "Cliente %s guardado exitosamente con el username %s" % (cli.nombre,cli.user)
                          cli.save()
                          ctx = {'mensaje':mensaje}
                          return HttpResponseRedirect('/')
                  else:
                          mensaje = "el formulario no es valido"
                          ctx = {'form': form, 'mensaje': mensaje}
                          return render_to_response('pymes/register.html', ctx, context_instance=RequestContext(request))
  
          else:
                  form = RegistrationForm()
                  context = {'form':form}
                  return render_to_response('pymes/register.html', context, context_instance=RequestContext(request))
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>class perfilCliente(models.base.ModelBase):
          def _prepare(self):
                  super(perfilCliente, self)._prepare()
                  def create_cliente_user_callback(sender, instance, created, **kwargs):
                          if created:
                                  self.objects.create(user=instance)
                  post_save.connect(create_cliente_user_callback, sender=User, weak=False)
  
  class cliente(models.Model):
          __metaclass__   = perfilCliente
          user            = models.OneToOneField(User)
          nombre          = models.CharField(max_length=200)
          apellidos       = models.CharField(max_length=200)
          mail            = models.CharField(max_length=200)
          empresa         = models.CharField(max_length=200)
          status          = models.BooleanField(default=True)
          plan            = models.ForeignKey('pymes.plan',null=True,blank=True)
          def __unicode__(self):
                  nombreCompleto = "%s %s"%(self.nombre,self.apellidos)
                  return nombreCompleto
  </code></pre>
  
  <p>register.html</p>
  
  <pre><code>{% extends "base.html" %}
  {% block title %} Login {% endblock %}
  {% block content %}
  {{ mensaje }}
  &lt;form action="" method="post"&gt;
  {% csrf_token %}
  {% if form.errors %}&lt;p&gt; Corregir los campos: &lt;/p&gt; {% endif %}
  &lt;div class="register_div"&gt;
          {% if form.nombre.errors %}&lt;p class="error"&gt;{{ form.nombre.errors }}&lt;/p&gt;{% endif %}
          &lt;p&gt;&lt;label for="nombre"{% if form.nombre.errors %} class="error"{% endif %}&gt;Nombre:&lt;/label&gt;{{ form.nombre }}&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="register_div"&gt;
          {% if form.apellido.errors %}&lt;p class="error"&gt;{{ form.apellido.errors }}&lt;/p&gt;{% endif %}
          &lt;p&gt;&lt;label for="apellido"{% if form.apellido.errors %} class="error"{% endif %}&gt;Apellido:&lt;/label&gt;{{ form.apellidos }}&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="register_div"&gt;
          {% if form.empresa.errors %}&lt;p class="error"&gt;{{ form.empresa.errors }}&lt;/p&gt;{% endif %}
          &lt;p&gt;&lt;label for="empresa"{% if form.empresa.errors %} class="error"{% endif %}&gt;Empresa:&lt;/label&gt; {{ form.empresa }}&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="register_div"&gt;
          {% if form.mail.errors %}&lt;p class="error"&gt;{{ form.mail.errors }}&lt;/p&gt;{% endif %}
          &lt;p&gt;&lt;label for="mail"{% if form.mail.errors %} class="error"{% endif %}&gt;Email:&lt;/label&gt; {{ form.mail }}&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="register_div"&gt;
          {% if form.username.errors %} &lt;p class="error"&gt;{{ form.username.errors }}&lt;/p&gt;{% endif %}
          &lt;p&gt;&lt;label for="username"{% if form.username.errors %} class="error"{% endif %}&gt;Username:&lt;/label&gt; {{ form.username }}&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="register_div"&gt;
          {% if form.password.errors %}&lt;p class="error"&gt;{{ form.password.errors }}&lt;/p&gt;{% endif %}
          &lt;p&gt;&lt;label for="password"{% if form.password.errors %} class="error"{% endif %}&gt;Password:&lt;/label&gt; {{ form.password }}&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="register_div"&gt;
          {% if form.passworduno.errors %}&lt;p class="error"&gt;{{ form.passworduno.errors }}&lt;/p&gt;{% endif %}
          &lt;p&gt;&lt;label for="password1"{% if form.passworduno.errors %} class="error"{% endif %}&gt;Verify Password:&lt;/label&gt;{{ form.passworduno }}&lt;/p&gt;
  &lt;/div&gt;
  &lt;p&gt;&lt;input type="submit" alt="register" /&gt;&lt;/p&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The pages works fine but, when send the form show me this error:</p>
  
  <pre><code>Request Method: POST
  Request URL: http://192.168.2.106:8000/register/
  
  Django Version: 1.4.5
  Python Version: 2.7.3
  Installed Applications:
  ('django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.sites',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'django.contrib.admin',
   'saas.apps.pymes',
   'django.contrib.admindocs')
  Installed Middleware:
  ('django.middleware.common.CommonMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware')
  
  
  Traceback:
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
    111.                         response = callback(request, *callback_args, **callback_kwargs)
  File "/root/project/saas/saas/apps/pymes/views.py" in register_view
    52.           cli.save()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
    463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
    551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
    203.         return insert_query(self.model, objs, fields, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
    1593.     return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
    912.             cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
    40.             return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py" in execute
    114.             return self.cursor.execute(query, args)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py" in execute
    174.             self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py" in defaulterrorhandler
    36.     raise errorclass, errorvalue
  
  Exception Type: IntegrityError at /register/
  Exception Value: (1062, "Duplicate entry '3' for key 'user_id'"
  </code></pre>
  
  <p>This create a new user properly in database with id=3</p>
  
  <pre><code>mysql&gt; select * from auth_user;
  +----+-----------+------------+-----------+--------------------+----------+----------+-----------+--------------+---------------------+---------------------+
  | id | username  | first_name | last_name | email              | password                                                                      | is_staff | is_active | is_superuser | last_login          | date_joined         |
  +----+-----------+------------+-----------+--------------------+-------------------------------------------------------------------------------+----------+-----------+--------------+---------------------+---------------------+
  |  1 | root      |            |           | sdfds@sdfa.com     | pass1 |        1 |         1 |            1 | 2013-04-01 07:49:58 | 2013-04-01 07:49:58 |
  |  2 | oespinoza |            |           | zdssd@yahoo.es | pass2 |        0 |         1 |            0 | 2013-04-01 07:50:46 | 2013-04-01 07:50:46 |
  |  3 | loco      |            |           | sdfsd              | pass3 |        0 |         1 |            0 | 2013-04-01 08:03:37 | 2013-04-01 08:03:37 |
  +----+-----------+------------+-----------+--------------------+---------+----------+-----------+--------------+---------------------+---------------------+
  </code></pre>
  
  <p>And the realtionship works fine too. But, in table "cliente" show only this:</p>
  
  <pre><code>mysql&gt; select * from pymes_cliente;
  +----+---------+--------+-----------+------+---------+--------+---------+
  | id | user_id | nombre | apellidos | mail | empresa | status | plan_id |
  +----+---------+--------+-----------+------+---------+--------+---------+
  |  1 |       1 |        |           |      |         |      1 |    NULL |
  |  2 |       2 |        |           |      |         |      1 |    NULL |
  |  4 |       3 |        |           |      |         |      1 |    NULL |
  +----+---------+--------+-----------+------+---------+--------+---------+
  </code></pre>
  
  <p>Help me please.</p>