title: How to save a file to a model using upload_to for dynamic paths
body: <p>I am trying to create a folder for each users to put their project in. So their file will have the path <code>..\project\id\filename</code>, <code>id</code> is the user <code>id</code> and <code>filename</code> is the name of the file. Now using the arguments allowed for <code>upload_to</code> (<code>instance</code> and <code>filename</code>) in the <code>Filefield</code>, I realize that <code>instance.id</code> will be <code>None</code> and the path to the file will be <code>..\project\None\filename</code> instead of <code>..\project\id\filename</code>.</p>
  
  <p>Now reading the <a href="https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.FileField.upload_to" rel="nofollow noreferrer">Django documentation upload_to</a> I saw this:</p>
  
  <blockquote>
    <p>In most cases, this object will not have been saved to the database
    yet, so if it uses the default AutoField, it might not yet have a
    value for its primary key field.</p>
  </blockquote>
  
  <p>My interpretation is that creating a new record and <code>user_directory_path</code> are not instantiated at the same time, that is, when I call <code>create</code> on <code>Project</code> model, <code>instance.id</code> will be <code>None</code>. My question is now, is there a way to get around this? While I see <code>upload_to</code> convenient, it is not necessarily convenient for dynamic path such as the one I am doing. I was thinking of creating the record, then adding the file path in an update, but I am in search of a way that can save everything in one step.</p>
  
  <pre><code>models.py
  def user_directory_path(instance, filename):
      # file will be uploaded to MEDIA_ROOT/user_&lt;id&gt;/&lt;filename&gt;
      return 'project/{0}/{1}'.format(instance.user.id, filename)
  
  
  class Project(models.Model):
  
       email = models.ForeignKey(User,
                            to_field="email",
                            max_length=50
      )
      title = models.CharField(max_length=100)
      date_created = models.DateTimeField(auto_now_add=True)
      updated = models.DateTimeField(auto_now=True)
      file = models.FileField(upload_to=user_directory_path, validators=[validate_file_type], null=True)
  </code></pre>
  
  <p>This is <code>views.py</code> when the form passes validation. Notice <code>user_directory_path</code> is called just before the <code>create</code>.</p>
  
  <pre><code>email = request.user.email
  title = request.POST.get('title', '')
  file = request.FILES['file']
  filename = file.name
  instance = Usermie.objects.get(email=request.user.email)
  
  # Save to model
  user_directory_path(instance=instance, filename=filename)
  Project.objects.create(
      title=title,  file=file,
  )
  </code></pre>