title: How to save a file to a django model's filefield so that django handle the upload_to things?
body: <p>I need to compose a file in views.py, and set the file to filefield in a createView. Here is how my code looks:</p>
  <p>models.py:</p>
  <pre class="lang-py prettyprint-override"><code>    class A(models.Model):
          data = models.CharField()
          file = models.FileField(upload_to='%Y/%m/%d/', blank=True, null=True)
  </code></pre>
  <p>views.py:</p>
  <pre class="lang-py prettyprint-override"><code>    class ACreateView(CreateView):
  
          def form_valid(self, form):
              new_file_path = compose_a_new_file() ## make new file saved somewhere.
              form.instance.file.path = new_file_path 
              result = super().form_valid(form)
              return result 
  </code></pre>
  <p>Here, <code>new_file_path</code> is the path of newly composed and saved in a folder.</p>
  <p>I just want to fill the file filefield with the newly composed file,
  and save it to the path defined by <code>upload_to='%Y/%m/%d/'</code>. (just as if I've uploaded by POST method from client)</p>
  <p>But the code above didn't save the file into <code>'%Y/%m/%d/'</code> folder.</p>
  <p>How can I get that?
  Any advice would be grateful.</p>