@@ -545,6 +545,45 @@ can change the maximum length using the :attr:`~CharField.max_length` argument.
545
545
546
546
.. _`strftime formatting`: http://docs.python.org/library/time.html#time.strftime
547
547
548
+ FileField and FieldFile
549
+ ~~~~~~~~~~~~~~~~~~~~~~~
550
+
551
+ When you access a :class:`FileField` on a model, you are given an instance
552
+ of :class:`FieldFile` as a proxy for accessing the underlying file. This
553
+ class has several methods that can be used to interact with file data:
554
+
555
+ .. method:: FieldFile.open(mode='rb')
556
+
557
+ Behaves like the standard Python ``open()`` method and opens the file
558
+ associated with this instance in the mode specified by ``mode``.
559
+
560
+ .. method:: FieldFile.close()
561
+
562
+ Behaves like the standard Python ``file.close()`` method and closes the file
563
+ associated with this instance.
564
+
565
+ .. method:: FieldFile.save(name, content, save=True)
566
+
567
+ This method takes a filename and file contents and passes them to the storage
568
+ class for the field, then associates the stored file with the model field.
569
+ If you want to manually associate file data with :class:`FileField`
570
+ instances on your model, the ``save()`` method is used to persist that file
571
+ data.
572
+
573
+ Takes two required arguments: ``name`` which is the name of the file, and
574
+ ``content`` which is a file-like object containing the file's contents. The
575
+ optional ``save`` argument controls whether or not the instance is saved after
576
+ the file has been altered. Defaults to ``True``.
577
+
578
+ .. method:: FieldFile.delete(save=True)
579
+
580
+ Deletes the file associated with this instance and clears all attributes on
581
+ the field. Note: This method will close the file if it happens to be open when
582
+ ``delete()`` is called.
583
+
584
+ The optional ``save`` argument controls whether or not the instance is saved
585
+ after the file has been deleted. Defaults to ``True``.
586
+
548
587
``FilePathField``
549
588
-----------------
550
589
@@ -605,8 +644,15 @@ The admin represents this as an ``<input type="text">`` (a single-line input).
605
644
606
645
.. class:: ImageField(upload_to=None, [height_field=None, width_field=None, max_length=100, **options])
607
646
608
- Like :class:`FileField`, but validates that the uploaded object is a valid
609
- image. Has two extra optional arguments:
647
+ Inherits all attributes and methods from :class:`FileField`, but also
648
+ validates that the uploaded object is a valid image.
649
+
650
+ In addition to the special attributes that are available for :class:`FileField`,
651
+ an :class:`ImageField` also has :attr:`~django.core.files.File.height` and
652
+ :attr:`~django.core.files.File.width` attributes.
653
+
654
+ To facilitate querying on those attributes, :class:`ImageField` has two extra
655
+ optional arguments:
610
656
611
657
.. attribute:: ImageField.height_field
612
658
@@ -618,20 +664,16 @@ image. Has two extra optional arguments:
618
664
Name of a model field which will be auto-populated with the width of the
619
665
image each time the model instance is saved.
620
666
621
- In addition to the special attributes that are available for :class:`FileField`,
622
- an :class:`ImageField` also has ``File.height`` and ``File.width`` attributes.
623
- See :ref:`topics-files`.
624
-
625
667
Requires the `Python Imaging Library`_.
626
668
627
669
.. _Python Imaging Library: http://www.pythonware.com/products/pil/
628
670
629
671
.. versionadded:: 1.0
630
672
The ``max_length`` argument was added in this version.
631
673
632
- By default, :class:`ImageField` instances are
633
- created as ``varchar(100)`` columns in your database. As with other fields, you
634
- can change the maximum length using the :attr:`~CharField.max_length` argument.
674
+ By default, :class:`ImageField` instances are created as ``varchar(100)``
675
+ columns in your database. As with other fields, you can change the maximum
676
+ length using the :attr:`~CharField.max_length` argument.
635
677
636
678
``IntegerField``
637
679
----------------
@@ -844,9 +886,9 @@ define the details of how the relation works.
844
886
current date/time to be chosen.
845
887
846
888
Instead of a dictionary this can also be a :class:`~django.db.models.Q`
847
- object for more :ref:`complex queries <complex-lookups-with-q>`. However,
848
- if ``limit_choices_to`` is a :class:`~django.db.models.Q` object then it
849
- will only have an effect on the choices available in the admin when the
889
+ object for more :ref:`complex queries <complex-lookups-with-q>`. However,
890
+ if ``limit_choices_to`` is a :class:`~django.db.models.Q` object then it
891
+ will only have an effect on the choices available in the admin when the
850
892
field is not listed in ``raw_id_fields`` in the ``ModelAdmin`` for the model.
851
893
852
894
.. attribute:: ForeignKey.related_name
0 commit comments