diff options
| author | Tim Graham <timograham@gmail.com> | 2013-07-05 06:53:19 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-07-05 06:54:34 -0400 |
| commit | 14a5b79e294506ab045a14fbec89030510f1cefe (patch) | |
| tree | b526134f661f059aa121b3be372c4d20cf1b1af6 /docs | |
| parent | 80dc8175a653eee5d7f09e5f357eac75bdab3a3b (diff) | |
[1.6.x] Fixed #19539 -- Updated custom model fields example for Python 3.
Thanks astorije@ for the report.
Backport of b9fceadfd4 from master.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/howto/custom-model-fields.txt | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/docs/howto/custom-model-fields.txt b/docs/howto/custom-model-fields.txt index 54913a887a..85c71004f4 100644 --- a/docs/howto/custom-model-fields.txt +++ b/docs/howto/custom-model-fields.txt @@ -249,7 +249,7 @@ appropriate Python object. The details of how this happens internally are a little complex, but the code you need to write in your ``Field`` class is simple: make sure your field subclass uses a special metaclass: -For example:: +For example, on Python 2:: class HandField(models.Field): @@ -258,7 +258,21 @@ For example:: __metaclass__ = models.SubfieldBase def __init__(self, *args, **kwargs): - # ... + ... + +On Python 3, in lieu of setting the ``__metaclass__`` attribute, add +``metaclass`` to the class definition:: + + class HandField(models.Field, metaclass=models.SubfieldBase): + ... + +If you want your code to work on Python 2 & 3, you can use +:func:`six.with_metaclass`:: + + from django.utils.six import with_metaclass + + class HandField(with_metaclass(models.SubfieldBase, models.Field)): + ... This ensures that the :meth:`.to_python` method, documented below, will always be called when the attribute is initialized. |
