[Django] #29607: detailed documentation/cookbook on how to handle files and their lifecycles in models

5 views
Skip to first unread message

Django

unread,
Jul 28, 2018, 12:48:11 PM7/28/18
to django-...@googlegroups.com
#29607: detailed documentation/cookbook on how to handle files and their lifecycles
in models
-------------------------------------------+------------------------
Reporter: Thomas Grainger | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
Currently the documentation on how to do various file operations isn't
documented eg:

* I've got some file contents as a string and a model instance with a file
field, I want to set the content of the instances' file to the value of
the string
* I've got a file like object and I'd like to transform it line by line
then append it to the file of a model field.

In all cases:

* properly managing the file lifetime open/close so that it works on non-
reference counting python implementations like PyPy
* cleaning up the FieldFile so that other code that accesses the file
still works
* never using up too much memory by reading a whole file into memory

Also, are some methods like: `instance.file.read()` without an associated
`with instance.file.open()` always dangerous - and should they be
deprecated?

Should the Django File instances operate more like `pathlib.PurePath` ? in
that `.read()` opens the file, reads the content then closes it, and
`.write()` opens the file, writes the file, then closes it.

--
Ticket URL: <https://code.djangoproject.com/ticket/29607>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 28, 2018, 3:16:39 PM7/28/18
to django-...@googlegroups.com
#29607: Add doc exmaples of handling files and their lifecycles in models
(FileField)
--------------------------------------+------------------------------------

Reporter: Thomas Grainger | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted
* component: Uncategorized => Documentation


Comment:

I guess `docs/topics/files.txt` might be the place for additional
documentation.

As for your questions, they seem better asked on the
DevelopersMailingList. As Python hasn't documented methods like `read()`,
I doubt Django would but maybe there's some nuance that I missed.

--
Ticket URL: <https://code.djangoproject.com/ticket/29607#comment:1>

Django

unread,
Jul 28, 2018, 3:22:35 PM7/28/18
to django-...@googlegroups.com
#29607: Add doc examples of handling files and their lifecycles with
FileField/models
--------------------------------------+------------------------------------

Reporter: Thomas Grainger | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/29607#comment:2>

Django

unread,
Apr 14, 2019, 9:33:59 AM4/14/19
to django-...@googlegroups.com
#29607: Add doc examples of handling files and their lifecycles with
FileField/models
-------------------------------------+-------------------------------------
Reporter: Thomas Grainger | Owner: Chris
Type: | Adams
Cleanup/optimization | Status: assigned

Component: Documentation | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Chris Adams):

* status: new => assigned
* cc: Chris Adams (added)
* owner: nobody => Chris Adams


Comment:

I'm happy to have a go at this.

I'm started in this branch here, adding some docs for common use cases
outlined above:

- set a file by passing the file path to the instance
- set a file by making a brand new file with ContentFile
- update a file by opening in a context manager, editing the file in
memory then saving
- work with a large file, using the __iter__ in a loop
- work with a large file, in chunks, using chunks()

I wasn't sure what this referred to though - can you give me any pointers?

> cleaning up the FieldFile so that other code that accesses the file
still works

https://github.com/django/django/pull/11227

For what it's worth, I'm really in favour of the pathlib
`instance.file.read()` syntax as outlined above - not having to faff with
context managers is really nice syntactic sugar.

--
Ticket URL: <https://code.djangoproject.com/ticket/29607#comment:3>

Django

unread,
Apr 15, 2019, 3:40:52 AM4/15/19
to django-...@googlegroups.com
#29607: Add doc examples of handling files and their lifecycles with
FileField/models.

-------------------------------------+-------------------------------------
Reporter: Thomas Grainger | Owner: Chris
Type: | Adams
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* version: 2.0 => master


Comment:

[https://github.com/django/django/pull/11227 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29607#comment:4>

Django

unread,
Apr 27, 2021, 3:30:15 PM4/27/21
to django-...@googlegroups.com
#29607: Add doc examples of handling files and their lifecycles with
FileField/models.
--------------------------------------+------------------------------------
Reporter: Thomas Grainger | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: dev

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* owner: Chris Adams => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/29607#comment:5>

Reply all
Reply to author
Forward
0 new messages