title: Get the URL of the file I just uploaded via form in Django
body: <p>I have this model with a FileField</p>
  <pre><code>class Attachment(models.Model):
      file = models.FileField(upload_to='%Y/%m/%d/')
      timestamp = models.DateTimeField(default=timezone.now)
  </code></pre>
  <p>Uploading is done with this form</p>
  <pre><code>class AttachmentForm(ModelForm):
      class Meta:
          model = Attachment
          fields = ['file']
  </code></pre>
  <p>and this js</p>
  <pre><code>function upload(event) {
    event.preventDefault()
    var data = new FormData($('#attachment_form').get(0))
    $.ajax({
      url: $(this).attr('action'),
      type: $(this).attr('method'),
      data: data,
      cache: false,
      processData: false,
      contentType: false,
    })
  
    $('#id_file').val('')
  
    // send message to server
    chatSocket.send(
      JSON.stringify({
        command: 'send',
        room: currentRoom,
        // message: ??
      })
    )
    return false
  }
  
  $(function () {
    $('#attachment_form').submit(upload)
  })
  </code></pre>
  <p>This part of my view handles the form</p>
  <pre><code># form to upload attachments
    if request.method == 'POST':
      attachment_form = AttachmentForm(request.POST, request.FILES)
      if attachment_form.is_valid():
        # file is saved
        attachment_form.save()
    else:
      attachment_form = AttachmentForm()
  </code></pre>
  <p>I am using this form inside a real-time chat application. What I want is the following: the user uploads a file, then the javascript will send a message to the websocket containing the URL of the newly uploaded file, so the server can broadcast the attachment to other users.</p>
  <p>How do I get the URL of the file I just uploaded? Keep in mind I can't just construct it with <code>MEDIA_ROOT</code> + whatever the <code>upload_to</code> field says + filename, as for example if two files with the same name are uploaded, Django will fill the second one with a few characters at the end of the name.</p>