title: ValueError: "needs to have a value for field "id" before this many-to-many relationship can be used"
body: <p>I have created an extended user model that I use on my sign-up page. The two custom models are "Cristin" and a many-to-many field named "Rolle" (the person's role in the organization).</p>
  
  <p>After I added the many-to-many field, I get the following error message: <strong>ValueError at /accounts/signup/ –"Userextended: <em>username_that_I_registered_with</em>" needs to have a value for field "id" before this many-to-many relationship can be used.</strong></p>
  
  <p>I have googled for three hours now, and it seems that the solution is to use <strong>commit=False</strong> on the User ID before saving the model. However, I'm new to Django and Python and have trouble pinpointing where I should write the code and my attempts has so far been unsuccessful. If you can help me solving this problem, please provide the solution through code as well.</p>
  
  <p>My <strong>models.py</strong> file:</p>
  
  <pre><code>class User(auth.models.User,auth.models.PermissionsMixin):
  def __str__(self):
      return "@{}".format(self.username)
  
  class Personrolle(models.Model):
      id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
      persrolle = models.CharField(max_length = 30, verbose_name="Role")
      def __str__(self):
          return self.persrolle
  
  class Userextended(models.Model):
      id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
      user = models.OneToOneField(User, on_delete=models.CASCADE)
      cristin = models.IntegerField(blank=True, null=True)
      rolle = models.ManyToManyField(Personrolle)
  
      def __str__(self):
          return self.user.username
  
  ...
  </code></pre>
  
  <p>My <strong>forms.py</strong> file:</p>
  
  <pre><code>...
  
  from django import forms
  from django.contrib.auth.models import User
  from accounts.models import Userextended
  from accounts.models import Personrolle
  from django.contrib.auth.forms import UserCreationForm
  class UserCreateForm(UserCreationForm):
  
      cristin = forms.IntegerField(required=False)
      rolle = forms.ModelChoiceField(queryset=Personrolle.objects.all())
  
      class Meta():
          fields = ('first_name','last_name','username','email','password1','password2')
          model = User
          labels = {'username': 'Email',
                    'first_name': 'First name',
                    'last_name': 'Last name',
                    'email': 'Confirm email',
                    'password1': 'Password',
                    'password2': 'Confirm password',
                    'cristin': 'Cristin-ID',
                    'rolle': 'Rolle'}
  
      def save(self, commit=True):
          if not commit:
              raise NotImplementedError("Can't create User and Userextended without database save")
          user = super(UserCreateForm, self).save(commit=True)
          user_profile = Userextended(user=user,cristin=self.cleaned_data['cristin'],rolle=self.cleaned_data['rolle'])
          user_profile.save()
          return user
  
  ...
  </code></pre>
  
  <p><strong>More detailed description of the error</strong></p>
  
  <ul>
  <li>Request Method:  POST</li>
  <li>Request URL: <a href="http://127.0.0.1:8000/accounts/signup/" rel="noreferrer">http://127.0.0.1:8000/accounts/signup/</a></li>
  <li>Django Version:  1.11.2</li>
  <li>Exception Type:  ValueError</li>
  <li><p>Exception Value: 
  "Userextended: username_that_I_registered_with" needs to have a value for field "id" before this many-to-many relationship can be used.</p></li>
  <li><p>Exception Location:  .../anaconda/envs/myEnv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py in <strong>init</strong>, line 830</p></li>
  <li><p>Python Executable:   .../anaconda/envs/myEnv/bin/python</p></li>
  <li>Python Version:  3.6.1</li>
  </ul>
  
  <p><strong>My traceback:</strong></p>
  
  <pre><code>File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
    41.             response = get_response(request)
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
    187.                 response = self.process_exception_by_middleware(e, request)
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
    185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/views/generic/base.py" in view
    68.             return self.dispatch(request, *args, **kwargs)
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch
    88.         return handler(request, *args, **kwargs)
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/views/generic/edit.py" in post
    217.         return super(BaseCreateView, self).post(request, *args, **kwargs)
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/views/generic/edit.py" in post
    183.             return self.form_valid(form)
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/views/generic/edit.py" in form_valid
    162.         self.object = form.save()
  
  File "…PROJECT/accounts/forms.py" in save
    57.         user_profile = Userextended(user=user,cristin=self.cleaned_data['cristin'],rolle=self.cleaned_data['rolle'])
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/db/models/base.py" in __init__
    566.                             _setattr(self, prop, kwargs[prop])
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py" in __set__
    536.         manager = self.__get__(instance)
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py" in __get__
    513.         return self.related_manager_cls(instance)
  
  File ".../anaconda/envs/myEnv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py" in __init__
    830.                                  (instance, self.pk_field_names[self.source_field_name]))
  
  Exception Type: ValueError at /accounts/signup/
  Exception Value: "&lt;Userextended: testytester@yahoo.com&gt;" needs to have a value for field "id" before this many-to-many relationship can be used.
  </code></pre>