title: Django jquery file upload: linking to another model
body: <p>Setting up django-jquery-fileupload <a href="https://github.com/sigurdga/django-jquery-file-upload" rel="nofollow noreferrer">https://github.com/sigurdga/django-jquery-file-upload</a> and it works, here's the model</p>
  
  <pre><code># encoding: utf-8
  from django.db import models
  
  
  class Picture(models.Model):
      """This is a small demo using just two fields. The slug field is really not
      necessary, but makes the code simpler. ImageField depends on PIL or
      pillow (where Pillow is easily installable in a virtualenv. If you have
      problems installing pillow, use a more generic FileField instead.
  
      """
      file = models.FileField(upload_to="uploads")
      slug = models.SlugField(max_length=50, blank=True)
  
      def __str__(self):
          return self.file.name
  
      @models.permalink
      def get_absolute_url(self):
          return ('upload-new', )
  
      def save(self, *args, **kwargs):
          self.slug = self.file.name
          super(Picture, self).save(*args, **kwargs)
  
      def delete(self, *args, **kwargs):
          """delete -- Remove to leave file."""
          self.file.delete(False)
          super(Picture, self).delete(*args, **kwargs)
  </code></pre>
  
  <p>and the view</p>
  
  <pre><code># encoding: utf-8
  import json
  
  from django.http import HttpResponse
  from django.views.generic import CreateView, DeleteView, ListView
  from .models import Picture
  from .response import JSONResponse, response_mimetype
  from .serialize import serialize
  
  
  class PictureCreateView(CreateView):
      model = Picture
      fields = "__all__"
  
      def form_valid(self, form):
          self.object = form.save()
          files = [serialize(self.object)]
          data = {'files': files}
          response = JSONResponse(data, mimetype=response_mimetype(self.request))
          response['Content-Disposition'] = 'inline; filename=files.json'
          return response
  
      def form_invalid(self, form):
          data = json.dumps(form.errors)
          return HttpResponse(content=data, status=400, content_type='application/json')
  </code></pre>
  
  <p>for now ignoring how behind the scenes it uploads the images, what I do however need to figure out is there is another model <code>UserService</code> which needs to be linked to <code>Picture</code> either using <code>ForeignKey</code> or <code>ManytoManyField</code> so the pictures are items on the userservice.</p>
  
  <p>If you have an idea on how to implement this so it works on upload.</p>