@@ -50,7 +50,7 @@ something like this::
5050
5151.. _Bridge: https://en.wikipedia.org/wiki/Contract_bridge
5252
53- This is just an ordinary Python class, with nothing Django-specific about it.
53+ This is an ordinary Python class, with nothing Django-specific about it.
5454We'd like to be able to do things like this in our models (we assume the
5555``hand`` attribute on the model is an instance of ``Hand``)::
5656
@@ -81,11 +81,12 @@ Background theory
8181Database storage
8282----------------
8383
84- The simplest way to think of a model field is that it provides a way to take a
85- normal Python object -- string, boolean, ``datetime``, or something more
86- complex like ``Hand`` -- and convert it to and from a format that is useful
87- when dealing with the database (and serialization, but, as we'll see later,
88- that falls out fairly naturally once you have the database side under control).
84+ Let's start with model fields. If you break it down, a model field provides a
85+ way to take a normal Python object -- string, boolean, ``datetime``, or
86+ something more complex like ``Hand`` -- and convert it to and from a format
87+ that is useful when dealing with the database. (Such a format is also useful
88+ for serialization, but as we'll see later, that is easier once you have the
89+ database side under control).
8990
9091Fields in a model must somehow be converted to fit into an existing database
9192column type. Different databases provide different sets of valid column types,
@@ -94,8 +95,7 @@ with. Anything you want to store in the database must fit into one of
9495those types.
9596
9697Normally, you're either writing a Django field to match a particular database
97- column type, or there's a fairly straightforward way to convert your data to,
98- say, a string.
98+ column type, or you will need a way to convert your data to, say, a string.
9999
100100For our ``Hand`` example, we could convert the card data to a string of 104
101101characters by concatenating all the cards together in a pre-determined order --
@@ -180,16 +180,16 @@ card values plus their suits; 104 characters in total.
180180 with. For example, you can pass both
181181 :attr:`~django.db.models.Field.editable` and
182182 :attr:`~django.db.models.DateField.auto_now` to a
183- :class:`django.db.models.DateField` and it will simply ignore the
183+ :class:`django.db.models.DateField` and it will ignore the
184184 :attr:`~django.db.models.Field.editable` parameter
185185 (:attr:`~django.db.models.DateField.auto_now` being set implies
186186 ``editable=False``). No error is raised in this case.
187187
188188 This behavior simplifies the field classes, because they don't need to
189- check for options that aren't necessary. They just pass all the options to
189+ check for options that aren't necessary. They pass all the options to
190190 the parent class and then don't use them later on. It's up to you whether
191191 you want your fields to be more strict about the options they select, or to
192- use the simpler, more permissive behavior of the current fields.
192+ use the more permissive behavior of the current fields.
193193
194194The ``Field.__init__()`` method takes the following parameters:
195195
@@ -241,11 +241,11 @@ then there's no need to write a new ``deconstruct()`` method. If, however,
241241you're changing the arguments passed in ``__init__()`` (like we are in
242242``HandField``), you'll need to supplement the values being passed.
243243
244- The contract of ``deconstruct()`` is simple; it returns a tuple of four items:
245- the field's attribute name, the full import path of the field class, the
246- positional arguments (as a list), and the keyword arguments (as a dict). Note
247- this is different from the ``deconstruct()`` method :ref:`for custom classes
248- <custom-deconstruct-method>` which returns a tuple of three things.
244+ ``deconstruct()`` returns a tuple of four items: the field's attribute name,
245+ the full import path of the field class, the positional arguments (as a list),
246+ and the keyword arguments (as a dict). Note this is different from the
247+ ``deconstruct()`` method :ref:`for custom classes <custom-deconstruct-method>`
248+ which returns a tuple of three things.
249249
250250As a custom field author, you don't need to care about the first two values;
251251the base ``Field`` class has all the code to work out the field's attribute
@@ -307,8 +307,8 @@ mind that people will be reconstructing your field from the serialized version
307307for quite a while (possibly years), depending how long your migrations live for.
308308
309309You can see the results of deconstruction by looking in migrations that include
310- the field, and you can test deconstruction in unit tests by just deconstructing
311- and reconstructing the field::
310+ the field, and you can test deconstruction in unit tests by deconstructing and
311+ reconstructing the field::
312312
313313 name, path, args, kwargs = my_field_instance.deconstruct()
314314 new_instance = MyField(*args, **kwargs)
@@ -349,10 +349,10 @@ As always, you should document your field type, so users will know what it is.
349349In addition to providing a docstring for it, which is useful for developers,
350350you can also allow users of the admin app to see a short description of the
351351field type via the :doc:`django.contrib.admindocs
352- </ref/contrib/admin/admindocs>` application. To do this simply provide
353- descriptive text in a :attr:`~Field.description` class attribute of your custom
354- field. In the above example, the description displayed by the ``admindocs``
355- application for a ``HandField`` will be 'A hand of cards (bridge style)'.
352+ </ref/contrib/admin/admindocs>` application. To do this provide descriptive
353+ text in a :attr:`~Field.description` class attribute of your custom field. In
354+ the above example, the description displayed by the ``admindocs`` application
355+ for a ``HandField`` will be 'A hand of cards (bridge style)'.
356356
357357In the :mod:`django.contrib.admindocs` display, the field description is
358358interpolated with ``field.__dict__`` which allows the description to
@@ -393,8 +393,8 @@ Once you have ``MytypeField``, you can use it in any model, just like any other
393393If you aim to build a database-agnostic application, you should account for
394394differences in database column types. For example, the date/time column type
395395in PostgreSQL is called ``timestamp``, while the same column in MySQL is called
396- ``datetime``. The simplest way to handle this in a :meth:`~Field.db_type`
397- method is to check the ``connection.settings_dict['ENGINE']`` attribute.
396+ ``datetime``. You can handle this in a :meth:`~Field.db_type` method by
397+ checking the ``connection.settings_dict['ENGINE']`` attribute.
398398
399399For example::
400400
@@ -431,7 +431,7 @@ sense to have a ``CharMaxlength25Field``, shown here::
431431 my_field = CharMaxlength25Field()
432432
433433The better way of doing this would be to make the parameter specifiable at run
434- time -- i.e., when the class is instantiated. To do that, just implement
434+ time -- i.e., when the class is instantiated. To do that, implement
435435``Field.__init__()``, like so::
436436
437437 # This is a much more flexible example.
@@ -730,7 +730,7 @@ accessed, and what methods are available. It lives at
730730:doc:`file documentation </ref/files/file>`.
731731
732732Once a subclass of ``File`` is created, the new ``FileField`` subclass must be
733- told to use it. To do so, simply assign the new ``File`` subclass to the special
733+ told to use it. To do so, assign the new ``File`` subclass to the special
734734``attr_class`` attribute of the ``FileField`` subclass.
735735
736736A few suggestions
0 commit comments