Opened 15 years ago

Closed 14 years ago

#11597 closed (fixed)

Documentation for File object is misleading

Reported by: amenasse@… Owned by: nobody
Component: Documentation Version: dev
Severity: Keywords: File url path
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I found the documentation for the File object misleading.

From http://docs.djangoproject.com/en/dev/topics/files/#topics-files

"
If you need to construct a File yourself, the easiest way is to create one using a Python built-in file object:


>>> from django.core.files import File

# Create a Python file object using open()
>>> f = open('/tmp/hello.world', 'w')
>>> myfile = File(f)

Now you can use any of the File attributes and methods documented in The File object."

The last statement is misleading because i found i couldn't:

http://docs.djangoproject.com/en/dev/ref/files/file/#ref-files-file

describes attributes such as url and path available to a File object

 >>> myfile.url
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'File' object has no attribute 'url'
>>> myfile.path
Traceback (most recent call last):
  File "<console>", line 1, in <module>

>>> 

i'm using svn release and docs.

as a side note , it would be really nice if things did work the way i understood from the docs.

My particular case requires me to create a File object that is not tied to a database field but has path and url attributes just like a FileField so from the templates i can do:

{{ object.url }}

For now i am hacking around this with my own class that provides these methods.

 class FakeFileField(object):
   
     def __init__(self,path):
       self.storage = FileSystemStorage()
       self._path = path

     def _get_url(self):
      return self.storage.url(self._path)

    def _get_path(self):
      return self.storage.path(self._path)

    .
    .

Thanks
Anthony

Change History (3)

comment:1 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Tim Graham, 15 years ago

Looks to me like the documentation is incorrect and that path and url are actually attributes on django.db.models.fields.FileField and not django.core.files.File

comment:3 by Gabriel Hurley, 14 years ago

Resolution: fixed
Status: newclosed

(In [14834]) [1.2.X] Fixed #13162 and #11597 -- Improved the file handling documentation: Removed documentation of methods on django.core.files.File that did not exist, added documentation for undocumented methods and attributes that did exist, did a general cleanup of the text and organization, and added more metadata targets. Thanks to amenasse and tyrion.mx for the reports.

Backport of [14833] from trunk.

Note: See TracTickets for help on using tickets.
Back to Top