196
196
If ``False``, the field will not be editable in the admin or via forms
197
197
automatically generated from the model class. Default is ``True``.
202
.. versionadded:: 1.2
204
.. attribute:: Field.error_messages
206
The ``error_messages`` argument lets you override the default messages that the
207
field will raise. Pass in a dictionary with keys matching the error messages you
299
321
primary key field will automatically be added to your model if you don't specify
300
322
otherwise. See :ref:`automatic-primary-key-fields`.
327
.. versionadded:: 1.2
329
.. class:: BigIntegerField([**options])
331
A 64 bit integer, much like an :class:`IntegerField` except that it is
332
guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. The
333
admin represents this as an ``<input type="text">`` (a single-line input).
309
343
The admin represents this as a checkbox.
311
.. admonition:: MySQL users..
345
.. versionchanged:: 1.2
313
A boolean field in MySQL is stored as a ``TINYINT`` column with a value of
314
either 0 or 1 (most databases have a proper ``BOOLEAN`` type instead). So,
315
for MySQL, only, when a ``BooleanField`` is retrieved from the database
316
and stored on a model attribute, it will have the values 1 or 0, rather
317
than ``True`` or ``False``. Normally, this shouldn't be a problem, since
318
Python guarantees that ``1 == True`` and ``0 == False`` are both true.
319
Just be careful if you're writing something like ``obj is True`` when
320
``obj`` is a value from a boolean attribute on a model. If that model was
321
constructed using the ``mysql`` backend, the "``is``" test will fail.
322
Prefer an equality test (using "``==``") in cases like this.
347
In previous versions of Django when running under MySQL ``BooleanFields``
348
would return their data as ``ints``, instead of true ``bools``. See the
349
release notes for a complete description of the change.
543
570
created as ``varchar(100)`` columns in your database. As with other fields, you
544
571
can change the maximum length using the :attr:`~CharField.max_length` argument.
546
.. _`strftime formatting`: http://docs.python.org/lib/module-time.html#l2h-1941
573
.. _`strftime formatting`: http://docs.python.org/library/time.html#time.strftime
575
FileField and FieldFile
576
~~~~~~~~~~~~~~~~~~~~~~~
578
When you access a :class:`FileField` on a model, you are given an instance
579
of :class:`FieldFile` as a proxy for accessing the underlying file. This
580
class has several methods that can be used to interact with file data:
582
.. method:: FieldFile.open(mode='rb')
584
Behaves like the standard Python ``open()`` method and opens the file
585
associated with this instance in the mode specified by ``mode``.
587
.. method:: FieldFile.close()
589
Behaves like the standard Python ``file.close()`` method and closes the file
590
associated with this instance.
592
.. method:: FieldFile.save(name, content, save=True)
594
This method takes a filename and file contents and passes them to the storage
595
class for the field, then associates the stored file with the model field.
596
If you want to manually associate file data with :class:`FileField`
597
instances on your model, the ``save()`` method is used to persist that file
600
Takes two required arguments: ``name`` which is the name of the file, and
601
``content`` which is a file-like object containing the file's contents. The
602
optional ``save`` argument controls whether or not the instance is saved after
603
the file has been altered. Defaults to ``True``.
605
.. method:: FieldFile.delete(save=True)
607
Deletes the file associated with this instance and clears all attributes on
608
the field. Note: This method will close the file if it happens to be open when
609
``delete()`` is called.
611
The optional ``save`` argument controls whether or not the instance is saved
612
after the file has been deleted. Defaults to ``True``.
548
614
``FilePathField``
549
615
-----------------
606
672
.. class:: ImageField(upload_to=None, [height_field=None, width_field=None, max_length=100, **options])
608
Like :class:`FileField`, but validates that the uploaded object is a valid
609
image. Has two extra optional arguments:
674
Inherits all attributes and methods from :class:`FileField`, but also
675
validates that the uploaded object is a valid image.
677
In addition to the special attributes that are available for :class:`FileField`,
678
an :class:`ImageField` also has :attr:`~django.core.files.File.height` and
679
:attr:`~django.core.files.File.width` attributes.
681
To facilitate querying on those attributes, :class:`ImageField` has two extra
611
684
.. attribute:: ImageField.height_field
618
691
Name of a model field which will be auto-populated with the width of the
619
692
image each time the model instance is saved.
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`.
625
694
Requires the `Python Imaging Library`_.
627
696
.. _Python Imaging Library: http://www.pythonware.com/products/pil/
629
698
.. versionadded:: 1.0
630
699
The ``max_length`` argument was added in this version.
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.
701
By default, :class:`ImageField` instances are created as ``varchar(100)``
702
columns in your database. As with other fields, you can change the maximum
703
length using the :attr:`~CharField.max_length` argument.
737
806
.. attribute:: URLField.verify_exists
739
808
If ``True`` (the default), the URL given will be checked for existence
740
(i.e., the URL actually loads and doesn't give a 404 response). It should
741
be noted that when using the single-threaded development server, validating
742
a url being serverd by the same server will hang.
743
This should not be a problem for multithreaded servers.
809
(i.e., the URL actually loads and doesn't give a 404 response).
811
Note that when you're using the single-threaded development server,
812
validating a URL being served by the same server will hang. This should not
813
be a problem for multithreaded servers.
745
815
The admin represents this as an ``<input type="text">`` (a single-line input).
844
914
current date/time to be chosen.
846
916
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>`.
849
``limit_choices_to`` has no effect on the inline FormSets that are created
850
to display related objects in the admin.
917
object for more :ref:`complex queries <complex-lookups-with-q>`. However,
918
if ``limit_choices_to`` is a :class:`~django.db.models.Q` object then it
919
will only have an effect on the choices available in the admin when the
920
field is not listed in ``raw_id_fields`` in the ``ModelAdmin`` for the model.
852
922
.. attribute:: ForeignKey.related_name
878
948
Database Representation
879
949
~~~~~~~~~~~~~~~~~~~~~~~
881
Behind the scenes, Django creates an intermediary join table to represent the
882
many-to-many relationship. By default, this table name is generated using the
883
names of the two tables being joined. Since some databases don't support table
884
names above a certain length, these table names will be automatically
885
truncated to 64 characters and a uniqueness hash will be used. This means you
886
might see table names like ``author_books_9cdf4``; this is perfectly normal.
951
Behind the scenes, Django creates an intermediary join table to
952
represent the many-to-many relationship. By default, this table name
953
is generated using the name of the many-to-many field and the model
954
that contains it. Since some databases don't support table names above
955
a certain length, these table names will be automatically truncated to
956
64 characters and a uniqueness hash will be used. This means you might
957
see table names like ``author_books_9cdf4``; this is perfectly normal.
887
958
You can manually provide the name of the join table using the
888
959
:attr:`~ManyToManyField.db_table` option.
906
977
``limit_choices_to`` has no effect when used on a ``ManyToManyField`` with a
907
978
custom intermediate table specified using the
908
:attr:`~ManyToManyField.through` paramter.
979
:attr:`~ManyToManyField.through` parameter.
910
981
.. attribute:: ManyToManyField.symmetrical