custom-model-fields.txt 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. ===========================
  2. Writing custom model fields
  3. ===========================
  4. .. currentmodule:: django.db.models
  5. Introduction
  6. ============
  7. The :doc:`model reference </topics/db/models>` documentation explains how to use
  8. Django's standard field classes -- :class:`~django.db.models.CharField`,
  9. :class:`~django.db.models.DateField`, etc. For many purposes, those classes are
  10. all you'll need. Sometimes, though, the Django version won't meet your precise
  11. requirements, or you'll want to use a field that is entirely different from
  12. those shipped with Django.
  13. Django's built-in field types don't cover every possible database column type --
  14. only the common types, such as ``VARCHAR`` and ``INTEGER``. For more obscure
  15. column types, such as geographic polygons or even user-created types such as
  16. `PostgreSQL custom types`_, you can define your own Django ``Field`` subclasses.
  17. .. _PostgreSQL custom types: http://www.postgresql.org/docs/current/interactive/sql-createtype.html
  18. Alternatively, you may have a complex Python object that can somehow be
  19. serialized to fit into a standard database column type. This is another case
  20. where a ``Field`` subclass will help you use your object with your models.
  21. Our example object
  22. ------------------
  23. Creating custom fields requires a bit of attention to detail. To make things
  24. easier to follow, we'll use a consistent example throughout this document:
  25. wrapping a Python object representing the deal of cards in a hand of Bridge_.
  26. Don't worry, you don't have to know how to play Bridge to follow this example.
  27. You only need to know that 52 cards are dealt out equally to four players, who
  28. are traditionally called *north*, *east*, *south* and *west*. Our class looks
  29. something like this::
  30. class Hand(object):
  31. """A hand of cards (bridge style)"""
  32. def __init__(self, north, east, south, west):
  33. # Input parameters are lists of cards ('Ah', '9s', etc)
  34. self.north = north
  35. self.east = east
  36. self.south = south
  37. self.west = west
  38. # ... (other possibly useful methods omitted) ...
  39. .. _Bridge: http://en.wikipedia.org/wiki/Contract_bridge
  40. This is just an ordinary Python class, with nothing Django-specific about it.
  41. We'd like to be able to do things like this in our models (we assume the
  42. ``hand`` attribute on the model is an instance of ``Hand``)::
  43. example = MyModel.objects.get(pk=1)
  44. print(example.hand.north)
  45. new_hand = Hand(north, east, south, west)
  46. example.hand = new_hand
  47. example.save()
  48. We assign to and retrieve from the ``hand`` attribute in our model just like
  49. any other Python class. The trick is to tell Django how to handle saving and
  50. loading such an object.
  51. In order to use the ``Hand`` class in our models, we **do not** have to change
  52. this class at all. This is ideal, because it means you can easily write
  53. model support for existing classes where you cannot change the source code.
  54. .. note::
  55. You might only be wanting to take advantage of custom database column
  56. types and deal with the data as standard Python types in your models;
  57. strings, or floats, for example. This case is similar to our ``Hand``
  58. example and we'll note any differences as we go along.
  59. Background theory
  60. =================
  61. Database storage
  62. ----------------
  63. The simplest way to think of a model field is that it provides a way to take a
  64. normal Python object -- string, boolean, ``datetime``, or something more
  65. complex like ``Hand`` -- and convert it to and from a format that is useful
  66. when dealing with the database (and serialization, but, as we'll see later,
  67. that falls out fairly naturally once you have the database side under control).
  68. Fields in a model must somehow be converted to fit into an existing database
  69. column type. Different databases provide different sets of valid column types,
  70. but the rule is still the same: those are the only types you have to work
  71. with. Anything you want to store in the database must fit into one of
  72. those types.
  73. Normally, you're either writing a Django field to match a particular database
  74. column type, or there's a fairly straightforward way to convert your data to,
  75. say, a string.
  76. For our ``Hand`` example, we could convert the card data to a string of 104
  77. characters by concatenating all the cards together in a pre-determined order --
  78. say, all the *north* cards first, then the *east*, *south* and *west* cards. So
  79. ``Hand`` objects can be saved to text or character columns in the database.
  80. What does a field class do?
  81. ---------------------------
  82. .. class:: Field
  83. All of Django's fields (and when we say *fields* in this document, we always
  84. mean model fields and not :doc:`form fields </ref/forms/fields>`) are subclasses
  85. of :class:`django.db.models.Field`. Most of the information that Django records
  86. about a field is common to all fields -- name, help text, uniqueness and so
  87. forth. Storing all that information is handled by ``Field``. We'll get into the
  88. precise details of what ``Field`` can do later on; for now, suffice it to say
  89. that everything descends from ``Field`` and then customizes key pieces of the
  90. class behavior.
  91. It's important to realize that a Django field class is not what is stored in
  92. your model attributes. The model attributes contain normal Python objects. The
  93. field classes you define in a model are actually stored in the ``Meta`` class
  94. when the model class is created (the precise details of how this is done are
  95. unimportant here). This is because the field classes aren't necessary when
  96. you're just creating and modifying attributes. Instead, they provide the
  97. machinery for converting between the attribute value and what is stored in the
  98. database or sent to the :doc:`serializer </topics/serialization>`.
  99. Keep this in mind when creating your own custom fields. The Django ``Field``
  100. subclass you write provides the machinery for converting between your Python
  101. instances and the database/serializer values in various ways (there are
  102. differences between storing a value and using a value for lookups, for
  103. example). If this sounds a bit tricky, don't worry -- it will become clearer in
  104. the examples below. Just remember that you will often end up creating two
  105. classes when you want a custom field:
  106. * The first class is the Python object that your users will manipulate.
  107. They will assign it to the model attribute, they will read from it for
  108. displaying purposes, things like that. This is the ``Hand`` class in our
  109. example.
  110. * The second class is the ``Field`` subclass. This is the class that knows
  111. how to convert your first class back and forth between its permanent
  112. storage form and the Python form.
  113. Writing a field subclass
  114. ========================
  115. When planning your :class:`~django.db.models.Field` subclass, first give some
  116. thought to which existing :class:`~django.db.models.Field` class your new field
  117. is most similar to. Can you subclass an existing Django field and save yourself
  118. some work? If not, you should subclass the :class:`~django.db.models.Field`
  119. class, from which everything is descended.
  120. Initializing your new field is a matter of separating out any arguments that are
  121. specific to your case from the common arguments and passing the latter to the
  122. ``__init__()`` method of :class:`~django.db.models.Field` (or your parent
  123. class).
  124. In our example, we'll call our field ``HandField``. (It's a good idea to call
  125. your :class:`~django.db.models.Field` subclass ``<Something>Field``, so it's
  126. easily identifiable as a :class:`~django.db.models.Field` subclass.) It doesn't
  127. behave like any existing field, so we'll subclass directly from
  128. :class:`~django.db.models.Field`::
  129. from django.db import models
  130. class HandField(models.Field):
  131. description = "A hand of cards (bridge style)"
  132. def __init__(self, *args, **kwargs):
  133. kwargs['max_length'] = 104
  134. super(HandField, self).__init__(*args, **kwargs)
  135. Our ``HandField`` accepts most of the standard field options (see the list
  136. below), but we ensure it has a fixed length, since it only needs to hold 52
  137. card values plus their suits; 104 characters in total.
  138. .. note::
  139. Many of Django's model fields accept options that they don't do anything
  140. with. For example, you can pass both
  141. :attr:`~django.db.models.Field.editable` and
  142. :attr:`~django.db.models.DateField.auto_now` to a
  143. :class:`django.db.models.DateField` and it will simply ignore the
  144. :attr:`~django.db.models.Field.editable` parameter
  145. (:attr:`~django.db.models.DateField.auto_now` being set implies
  146. ``editable=False``). No error is raised in this case.
  147. This behavior simplifies the field classes, because they don't need to
  148. check for options that aren't necessary. They just pass all the options to
  149. the parent class and then don't use them later on. It's up to you whether
  150. you want your fields to be more strict about the options they select, or to
  151. use the simpler, more permissive behavior of the current fields.
  152. .. method:: Field.__init__
  153. The :meth:`~django.db.models.Field.__init__` method takes the following
  154. parameters:
  155. * :attr:`~django.db.models.Field.verbose_name`
  156. * ``name``
  157. * :attr:`~django.db.models.Field.primary_key`
  158. * :attr:`~django.db.models.CharField.max_length`
  159. * :attr:`~django.db.models.Field.unique`
  160. * :attr:`~django.db.models.Field.blank`
  161. * :attr:`~django.db.models.Field.null`
  162. * :attr:`~django.db.models.Field.db_index`
  163. * ``rel``: Used for related fields (like :class:`ForeignKey`). For advanced
  164. use only.
  165. * :attr:`~django.db.models.Field.default`
  166. * :attr:`~django.db.models.Field.editable`
  167. * ``serialize``: If ``False``, the field will not be serialized when the model
  168. is passed to Django's :doc:`serializers </topics/serialization>`. Defaults to
  169. ``True``.
  170. * :attr:`~django.db.models.Field.unique_for_date`
  171. * :attr:`~django.db.models.Field.unique_for_month`
  172. * :attr:`~django.db.models.Field.unique_for_year`
  173. * :attr:`~django.db.models.Field.choices`
  174. * :attr:`~django.db.models.Field.help_text`
  175. * :attr:`~django.db.models.Field.db_column`
  176. * :attr:`~django.db.models.Field.db_tablespace`: Only for index creation, if the
  177. backend supports :doc:`tablespaces </topics/db/tablespaces>`. You can usually
  178. ignore this option.
  179. * ``auto_created``: ``True`` if the field was automatically created, as for the
  180. :class:`~django.db.models.OneToOneField` used by model inheritance. For
  181. advanced use only.
  182. All of the options without an explanation in the above list have the same
  183. meaning they do for normal Django fields. See the :doc:`field documentation
  184. </ref/models/fields>` for examples and details.
  185. The ``SubfieldBase`` metaclass
  186. ------------------------------
  187. .. class:: django.db.models.SubfieldBase
  188. As we indicated in the introduction_, field subclasses are often needed for
  189. two reasons: either to take advantage of a custom database column type, or to
  190. handle complex Python types. Obviously, a combination of the two is also
  191. possible. If you're only working with custom database column types and your
  192. model fields appear in Python as standard Python types direct from the
  193. database backend, you don't need to worry about this section.
  194. If you're handling custom Python types, such as our ``Hand`` class, we need to
  195. make sure that when Django initializes an instance of our model and assigns a
  196. database value to our custom field attribute, we convert that value into the
  197. appropriate Python object. The details of how this happens internally are a
  198. little complex, but the code you need to write in your ``Field`` class is
  199. simple: make sure your field subclass uses a special metaclass:
  200. For example, on Python 2::
  201. class HandField(models.Field):
  202. description = "A hand of cards (bridge style)"
  203. __metaclass__ = models.SubfieldBase
  204. def __init__(self, *args, **kwargs):
  205. ...
  206. On Python 3, in lieu of setting the ``__metaclass__`` attribute, add
  207. ``metaclass`` to the class definition::
  208. class HandField(models.Field, metaclass=models.SubfieldBase):
  209. ...
  210. If you want your code to work on Python 2 & 3, you can use
  211. :func:`six.with_metaclass`::
  212. from django.utils.six import with_metaclass
  213. class HandField(with_metaclass(models.SubfieldBase, models.Field)):
  214. ...
  215. This ensures that the :meth:`.to_python` method, documented below, will always
  216. be called when the attribute is initialized.
  217. ModelForms and custom fields
  218. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  219. If you use :class:`~django.db.models.SubfieldBase`, :meth:`.to_python`
  220. will be called every time an instance of the field is assigned a
  221. value. This means that whenever a value may be assigned to the field,
  222. you need to ensure that it will be of the correct datatype, or that
  223. you handle any exceptions.
  224. This is especially important if you use :doc:`ModelForms
  225. </topics/forms/modelforms>`. When saving a ModelForm, Django will use
  226. form values to instantiate model instances. However, if the cleaned
  227. form data can't be used as valid input to the field, the normal form
  228. validation process will break.
  229. Therefore, you must ensure that the form field used to represent your
  230. custom field performs whatever input validation and data cleaning is
  231. necessary to convert user-provided form input into a
  232. ``to_python()``-compatible model field value. This may require writing a
  233. custom form field, and/or implementing the :meth:`.formfield` method on
  234. your field to return a form field class whose ``to_python()`` returns the
  235. correct datatype.
  236. Documenting your custom field
  237. -----------------------------
  238. .. attribute:: Field.description
  239. As always, you should document your field type, so users will know what it is.
  240. In addition to providing a docstring for it, which is useful for developers,
  241. you can also allow users of the admin app to see a short description of the
  242. field type via the :doc:`django.contrib.admindocs
  243. </ref/contrib/admin/admindocs>` application. To do this simply provide
  244. descriptive text in a ``description`` class attribute of your custom field. In
  245. the above example, the description displayed by the ``admindocs``
  246. application for a ``HandField`` will be 'A hand of cards (bridge style)'.
  247. In the :mod:`django.contrib.admindocs` display, the field description is
  248. interpolated with ``field.__dict__`` which allows the description to
  249. incorporate arguments of the field. For example, the description for
  250. :class:`~django.db.models.CharField` is::
  251. description = _("String (up to %(max_length)s)")
  252. Useful methods
  253. --------------
  254. Once you've created your :class:`~django.db.models.Field` subclass and set up
  255. the ``__metaclass__``, you might consider overriding a few standard methods,
  256. depending on your field's behavior. The list of methods below is in
  257. approximately decreasing order of importance, so start from the top.
  258. Custom database types
  259. ~~~~~~~~~~~~~~~~~~~~~
  260. .. method:: Field.db_type(self, connection)
  261. Returns the database column data type for the :class:`~django.db.models.Field`,
  262. taking into account the connection object, and the settings associated with it.
  263. Say you've created a PostgreSQL custom type called ``mytype``. You can use this
  264. field with Django by subclassing ``Field`` and implementing the
  265. :meth:`.db_type` method, like so::
  266. from django.db import models
  267. class MytypeField(models.Field):
  268. def db_type(self, connection):
  269. return 'mytype'
  270. Once you have ``MytypeField``, you can use it in any model, just like any other
  271. ``Field`` type::
  272. class Person(models.Model):
  273. name = models.CharField(max_length=80)
  274. something_else = MytypeField()
  275. If you aim to build a database-agnostic application, you should account for
  276. differences in database column types. For example, the date/time column type
  277. in PostgreSQL is called ``timestamp``, while the same column in MySQL is called
  278. ``datetime``. The simplest way to handle this in a :meth:`.db_type`
  279. method is to check the ``connection.settings_dict['ENGINE']`` attribute.
  280. For example::
  281. class MyDateField(models.Field):
  282. def db_type(self, connection):
  283. if connection.settings_dict['ENGINE'] == 'django.db.backends.mysql':
  284. return 'datetime'
  285. else:
  286. return 'timestamp'
  287. The :meth:`.db_type` method is only called by Django when the framework
  288. constructs the ``CREATE TABLE`` statements for your application -- that is,
  289. when you first create your tables. It's not called at any other time, so it can
  290. afford to execute slightly complex code, such as the
  291. ``connection.settings_dict`` check in the above example.
  292. Some database column types accept parameters, such as ``CHAR(25)``, where the
  293. parameter ``25`` represents the maximum column length. In cases like these,
  294. it's more flexible if the parameter is specified in the model rather than being
  295. hard-coded in the ``db_type()`` method. For example, it wouldn't make much
  296. sense to have a ``CharMaxlength25Field``, shown here::
  297. # This is a silly example of hard-coded parameters.
  298. class CharMaxlength25Field(models.Field):
  299. def db_type(self, connection):
  300. return 'char(25)'
  301. # In the model:
  302. class MyModel(models.Model):
  303. # ...
  304. my_field = CharMaxlength25Field()
  305. The better way of doing this would be to make the parameter specifiable at run
  306. time -- i.e., when the class is instantiated. To do that, just implement
  307. :meth:`django.db.models.Field.__init__`, like so::
  308. # This is a much more flexible example.
  309. class BetterCharField(models.Field):
  310. def __init__(self, max_length, *args, **kwargs):
  311. self.max_length = max_length
  312. super(BetterCharField, self).__init__(*args, **kwargs)
  313. def db_type(self, connection):
  314. return 'char(%s)' % self.max_length
  315. # In the model:
  316. class MyModel(models.Model):
  317. # ...
  318. my_field = BetterCharField(25)
  319. Finally, if your column requires truly complex SQL setup, return ``None`` from
  320. :meth:`.db_type`. This will cause Django's SQL creation code to skip
  321. over this field. You are then responsible for creating the column in the right
  322. table in some other way, of course, but this gives you a way to tell Django to
  323. get out of the way.
  324. Converting database values to Python objects
  325. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  326. .. method:: Field.to_python(self, value)
  327. Converts a value as returned by your database (or a serializer) to a Python
  328. object.
  329. The default implementation simply returns ``value``, for the common case in
  330. which the database backend already returns data in the correct format (as a
  331. Python string, for example).
  332. If your custom :class:`~django.db.models.Field` class deals with data structures
  333. that are more complex than strings, dates, integers or floats, then you'll need
  334. to override this method. As a general rule, the method should deal gracefully
  335. with any of the following arguments:
  336. * An instance of the correct type (e.g., ``Hand`` in our ongoing example).
  337. * A string (e.g., from a deserializer).
  338. * Whatever the database returns for the column type you're using.
  339. In our ``HandField`` class, we're storing the data as a VARCHAR field in the
  340. database, so we need to be able to process strings and ``Hand`` instances in
  341. :meth:`.to_python`::
  342. import re
  343. class HandField(models.Field):
  344. # ...
  345. def to_python(self, value):
  346. if isinstance(value, Hand):
  347. return value
  348. # The string case.
  349. p1 = re.compile('.{26}')
  350. p2 = re.compile('..')
  351. args = [p2.findall(x) for x in p1.findall(value)]
  352. if len(args) != 4:
  353. raise ValidationError("Invalid input for a Hand instance")
  354. return Hand(*args)
  355. Notice that we always return a ``Hand`` instance from this method. That's the
  356. Python object type we want to store in the model's attribute. If anything is
  357. going wrong during value conversion, you should raise a
  358. :exc:`~django.core.exceptions.ValidationError` exception.
  359. **Remember:** If your custom field needs the :meth:`.to_python` method to be
  360. called when it is created, you should be using `The SubfieldBase metaclass`_
  361. mentioned earlier. Otherwise :meth:`.to_python` won't be called
  362. automatically.
  363. .. warning::
  364. If your custom field allows ``null=True``, any field method that takes
  365. ``value`` as an argument, like :meth:`~Field.to_python` and
  366. :meth:`~Field.get_prep_value`, should handle the case when ``value`` is
  367. ``None``.
  368. Converting Python objects to query values
  369. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  370. .. method:: Field.get_prep_value(self, value)
  371. This is the reverse of :meth:`.to_python` when working with the
  372. database backends (as opposed to serialization). The ``value``
  373. parameter is the current value of the model's attribute (a field has
  374. no reference to its containing model, so it cannot retrieve the value
  375. itself), and the method should return data in a format that has been
  376. prepared for use as a parameter in a query.
  377. This conversion should *not* include any database-specific
  378. conversions. If database-specific conversions are required, they
  379. should be made in the call to :meth:`.get_db_prep_value`.
  380. For example::
  381. class HandField(models.Field):
  382. # ...
  383. def get_prep_value(self, value):
  384. return ''.join([''.join(l) for l in (value.north,
  385. value.east, value.south, value.west)])
  386. Converting query values to database values
  387. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  388. .. method:: Field.get_db_prep_value(self, value, connection, prepared=False)
  389. Some data types (for example, dates) need to be in a specific format
  390. before they can be used by a database backend.
  391. :meth:`.get_db_prep_value` is the method where those conversions should
  392. be made. The specific connection that will be used for the query is
  393. passed as the ``connection`` parameter. This allows you to use
  394. backend-specific conversion logic if it is required.
  395. The ``prepared`` argument describes whether or not the value has
  396. already been passed through :meth:`.get_prep_value` conversions. When
  397. ``prepared`` is False, the default implementation of
  398. :meth:`.get_db_prep_value` will call :meth:`.get_prep_value` to do
  399. initial data conversions before performing any database-specific
  400. processing.
  401. .. method:: Field.get_db_prep_save(self, value, connection)
  402. Same as the above, but called when the Field value must be *saved* to
  403. the database. As the default implementation just calls
  404. :meth:`.get_db_prep_value`, you shouldn't need to implement this method
  405. unless your custom field needs a special conversion when being saved
  406. that is not the same as the conversion used for normal query
  407. parameters (which is implemented by :meth:`.get_db_prep_value`).
  408. Preprocessing values before saving
  409. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  410. .. method:: Field.pre_save(self, model_instance, add)
  411. This method is called just prior to :meth:`.get_db_prep_save` and should return
  412. the value of the appropriate attribute from ``model_instance`` for this field.
  413. The attribute name is in ``self.attname`` (this is set up by
  414. :class:`~django.db.models.Field`). If the model is being saved to the database
  415. for the first time, the ``add`` parameter will be ``True``, otherwise it will be
  416. ``False``.
  417. You only need to override this method if you want to preprocess the value
  418. somehow, just before saving. For example, Django's
  419. :class:`~django.db.models.DateTimeField` uses this method to set the attribute
  420. correctly in the case of :attr:`~django.db.models.DateField.auto_now` or
  421. :attr:`~django.db.models.DateField.auto_now_add`.
  422. If you do override this method, you must return the value of the attribute at
  423. the end. You should also update the model's attribute if you make any changes
  424. to the value so that code holding references to the model will always see the
  425. correct value.
  426. Preparing values for use in database lookups
  427. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  428. As with value conversions, preparing a value for database lookups is a
  429. two phase process.
  430. .. method:: Field.get_prep_lookup(self, lookup_type, value)
  431. :meth:`.get_prep_lookup` performs the first phase of lookup preparation,
  432. performing generic data validity checks
  433. Prepares the ``value`` for passing to the database when used in a lookup (a
  434. ``WHERE`` constraint in SQL). The ``lookup_type`` will be one of the valid
  435. Django filter lookups: ``exact``, ``iexact``, ``contains``, ``icontains``,
  436. ``gt``, ``gte``, ``lt``, ``lte``, ``in``, ``startswith``, ``istartswith``,
  437. ``endswith``, ``iendswith``, ``range``, ``year``, ``month``, ``day``,
  438. ``isnull``, ``search``, ``regex``, and ``iregex``.
  439. Your method must be prepared to handle all of these ``lookup_type`` values and
  440. should raise either a ``ValueError`` if the ``value`` is of the wrong sort (a
  441. list when you were expecting an object, for example) or a ``TypeError`` if
  442. your field does not support that type of lookup. For many fields, you can get
  443. by with handling the lookup types that need special handling for your field
  444. and pass the rest to the :meth:`.get_db_prep_lookup` method of the parent class.
  445. If you needed to implement ``get_db_prep_save()``, you will usually need to
  446. implement ``get_prep_lookup()``. If you don't, ``get_prep_value`` will be
  447. called by the default implementation, to manage ``exact``, ``gt``, ``gte``,
  448. ``lt``, ``lte``, ``in`` and ``range`` lookups.
  449. You may also want to implement this method to limit the lookup types that could
  450. be used with your custom field type.
  451. Note that, for ``range`` and ``in`` lookups, ``get_prep_lookup`` will receive
  452. a list of objects (presumably of the right type) and will need to convert them
  453. to a list of things of the right type for passing to the database. Most of the
  454. time, you can reuse ``get_prep_value()``, or at least factor out some common
  455. pieces.
  456. For example, the following code implements ``get_prep_lookup`` to limit the
  457. accepted lookup types to ``exact`` and ``in``::
  458. class HandField(models.Field):
  459. # ...
  460. def get_prep_lookup(self, lookup_type, value):
  461. # We only handle 'exact' and 'in'. All others are errors.
  462. if lookup_type == 'exact':
  463. return self.get_prep_value(value)
  464. elif lookup_type == 'in':
  465. return [self.get_prep_value(v) for v in value]
  466. else:
  467. raise TypeError('Lookup type %r not supported.' % lookup_type)
  468. .. method:: Field.get_db_prep_lookup(self, lookup_type, value, connection, prepared=False)
  469. Performs any database-specific data conversions required by a lookup.
  470. As with :meth:`.get_db_prep_value`, the specific connection that will
  471. be used for the query is passed as the ``connection`` parameter.
  472. The ``prepared`` argument describes whether the value has already been
  473. prepared with :meth:`.get_prep_lookup`.
  474. Specifying the form field for a model field
  475. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  476. .. method:: Field.formfield(self, form_class=forms.CharField, **kwargs)
  477. Returns the default form field to use when this field is displayed in a model.
  478. This method is called by the :class:`~django.forms.ModelForm` helper.
  479. All of the ``kwargs`` dictionary is passed directly to the form field's
  480. ``__init__()`` method. Normally, all you need to do is set up a good default
  481. for the ``form_class`` argument and then delegate further handling to the
  482. parent class. This might require you to write a custom form field (and even a
  483. form widget). See the :doc:`forms documentation </topics/forms/index>` for
  484. information about this.
  485. Continuing our ongoing example, we can write the :meth:`.formfield` method as::
  486. class HandField(models.Field):
  487. # ...
  488. def formfield(self, **kwargs):
  489. # This is a fairly standard way to set up some defaults
  490. # while letting the caller override them.
  491. defaults = {'form_class': MyFormField}
  492. defaults.update(kwargs)
  493. return super(HandField, self).formfield(**defaults)
  494. This assumes we've imported a ``MyFormField`` field class (which has its own
  495. default widget). This document doesn't cover the details of writing custom form
  496. fields.
  497. .. _helper functions: ../forms/#generating-forms-for-models
  498. .. _forms documentation: ../forms/
  499. Emulating built-in field types
  500. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  501. .. method:: Field.get_internal_type(self)
  502. Returns a string giving the name of the :class:`~django.db.models.Field`
  503. subclass we are emulating at the database level. This is used to determine the
  504. type of database column for simple cases.
  505. If you have created a :meth:`.db_type` method, you don't need to worry about
  506. :meth:`.get_internal_type` -- it won't be used much. Sometimes, though, your
  507. database storage is similar in type to some other field, so you can use that
  508. other field's logic to create the right column.
  509. For example::
  510. class HandField(models.Field):
  511. # ...
  512. def get_internal_type(self):
  513. return 'CharField'
  514. No matter which database backend we are using, this will mean that ``syncdb``
  515. and other SQL commands create the right column type for storing a string.
  516. If :meth:`.get_internal_type` returns a string that is not known to Django for
  517. the database backend you are using -- that is, it doesn't appear in
  518. ``django.db.backends.<db_name>.creation.DATA_TYPES`` -- the string will still be
  519. used by the serializer, but the default :meth:`.db_type` method will return
  520. ``None``. See the documentation of :meth:`.db_type` for reasons why this might be
  521. useful. Putting a descriptive string in as the type of the field for the
  522. serializer is a useful idea if you're ever going to be using the serializer
  523. output in some other place, outside of Django.
  524. Converting field data for serialization
  525. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  526. .. method:: Field.value_to_string(self, obj)
  527. This method is used by the serializers to convert the field into a string for
  528. output. Calling ``Field._get_val_from_obj(obj)`` is the best way to get the
  529. value to serialize. For example, since our ``HandField`` uses strings for its
  530. data storage anyway, we can reuse some existing conversion code::
  531. class HandField(models.Field):
  532. # ...
  533. def value_to_string(self, obj):
  534. value = self._get_val_from_obj(obj)
  535. return self.get_prep_value(value)
  536. Some general advice
  537. --------------------
  538. Writing a custom field can be a tricky process, particularly if you're doing
  539. complex conversions between your Python types and your database and
  540. serialization formats. Here are a couple of tips to make things go more
  541. smoothly:
  542. 1. Look at the existing Django fields (in
  543. :file:`django/db/models/fields/__init__.py`) for inspiration. Try to find
  544. a field that's similar to what you want and extend it a little bit,
  545. instead of creating an entirely new field from scratch.
  546. 2. Put a ``__str__()`` or ``__unicode__()`` method on the class you're
  547. wrapping up as a field. There are a lot of places where the default
  548. behavior of the field code is to call
  549. :func:`~django.utils.encoding.force_text` on the value. (In our
  550. examples in this document, ``value`` would be a ``Hand`` instance, not a
  551. ``HandField``). So if your ``__unicode__()`` method (``__str__()`` on
  552. Python 3) automatically converts to the string form of your Python object,
  553. you can save yourself a lot of work.
  554. Writing a ``FileField`` subclass
  555. =================================
  556. In addition to the above methods, fields that deal with files have a few other
  557. special requirements which must be taken into account. The majority of the
  558. mechanics provided by ``FileField``, such as controlling database storage and
  559. retrieval, can remain unchanged, leaving subclasses to deal with the challenge
  560. of supporting a particular type of file.
  561. Django provides a ``File`` class, which is used as a proxy to the file's
  562. contents and operations. This can be subclassed to customize how the file is
  563. accessed, and what methods are available. It lives at
  564. ``django.db.models.fields.files``, and its default behavior is explained in the
  565. :doc:`file documentation </ref/files/file>`.
  566. Once a subclass of ``File`` is created, the new ``FileField`` subclass must be
  567. told to use it. To do so, simply assign the new ``File`` subclass to the special
  568. ``attr_class`` attribute of the ``FileField`` subclass.
  569. A few suggestions
  570. ------------------
  571. In addition to the above details, there are a few guidelines which can greatly
  572. improve the efficiency and readability of the field's code.
  573. 1. The source for Django's own ``ImageField`` (in
  574. ``django/db/models/fields/files.py``) is a great example of how to
  575. subclass ``FileField`` to support a particular type of file, as it
  576. incorporates all of the techniques described above.
  577. 2. Cache file attributes wherever possible. Since files may be stored in
  578. remote storage systems, retrieving them may cost extra time, or even
  579. money, that isn't always necessary. Once a file is retrieved to obtain
  580. some data about its content, cache as much of that data as possible to
  581. reduce the number of times the file must be retrieved on subsequent
  582. calls for that information.