title: Django - get file from db by its url
body: <p>I am trying to implement a secure access to files. This is my view serving static files:</p>
  
  <pre><code>from django.views.static import serve
  
  class ServeStatic(LoginRequiredMixin, View):
      login_url = reverse_lazy("login")
  
      def dispatch(self, request, *args, **kwargs):
          if request.user == Document.objects.get(url=self.kwargs["path"]).owner:
              return serve(request, kwargs["path"], kwargs["file_root"])
          else:
              return HttpResponseNotFound()
  </code></pre>
  
  <p>The point is that I cannot get the condition to work. Is it even possible to get a file by its URL, or I need to choose a very different approach of serving files?</p>
  
  <p>My Document model: </p>
  
  <pre><code> class Document(models.Model):
       owner = models.ForeignKey(settings.AUTH_USER_MODEL,
                                 verbose_name=_("Owner"), related_name="owner")
       document = models.FileField(upload_to=file_upload_handler, verbose_name=_("Document"))
  </code></pre>
  
  <p>Thanks for your ideas!</p>