title: Django: Access primary key in models.filefield(upload_to) location
body: <p>I'd like to save my files using the primary key of the entry.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>def get_nzb_filename(instance, filename):
      if not instance.pk:
          instance.save() # Does not work.
      name_slug = re.sub('[^a-zA-Z0-9]', '-', instance.name).strip('-').lower()
      name_slug = re.sub('[-]+', '-', name_slug)
      return u'files/%s_%s.nzb' % (instance.pk, name_slug)
  
  class File(models.Model):
      nzb = models.FileField(upload_to=get_nzb_filename)
      name = models.CharField(max_length=256)
  </code></pre>
  
  <p>I know the first time an object is saved the primary key isn't available, so I'm willing to take the extra hit to save the object just to get the primary key, and then continue on. </p>
  
  <p>The above code doesn't work. It throws the following error:</p>
  
  <pre><code>maximum recursion depth exceeded while calling a Python object
  </code></pre>
  
  <p>I'm assuming this is an infinite loop. Calling the <code>save</code> method would call the <code>get_nzb_filename</code> method, which would again call the <code>save</code> method, and so on.</p>
  
  <p>I'm using the latest version of the Django trunk.</p>
  
  <p>How can I get the primary key so I can use it to save my uploaded files?</p>
  
  <hr>
  
  <p><strong>Update @muhuk:</strong></p>
  
  <p>I like your solution. Can you help me implement it? I've updated my code to the following and the error is <code>'File' object has no attribute 'create'</code>. Perhaps I'm using what you've written out of context?</p>
  
  <pre><code>def create_with_pk(self):
      instance = self.create()
      instance.save()
      return instance
  
  def get_nzb_filename(instance, filename):
      if not instance.pk:
          create_with_pk(instance)
      name_slug = re.sub('[^a-zA-Z0-9]', '-', instance.name).strip('-').lower()
      name_slug = re.sub('[-]+', '-', name_slug)
      return u'files/%s_%s.nzb' % (instance.pk, name_slug)
  
  class File(models.Model):
      nzb = models.FileField(upload_to=get_nzb_filename, blank=True, null=True)
      name = models.CharField(max_length=256)
  </code></pre>
  
  <p>Instead of enforcing the required field in my model I'll do it in my Form class. No problem.</p>