Fixed #19539 -- Updated custom model fields example for Python 3.
Thanks astorije@ for the report.
This commit is contained in:
parent
3e60cc2992
commit
b9fceadfd4
|
@ -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
|
little complex, but the code you need to write in your ``Field`` class is
|
||||||
simple: make sure your field subclass uses a special metaclass:
|
simple: make sure your field subclass uses a special metaclass:
|
||||||
|
|
||||||
For example::
|
For example, on Python 2::
|
||||||
|
|
||||||
class HandField(models.Field):
|
class HandField(models.Field):
|
||||||
|
|
||||||
|
@ -258,7 +258,21 @@ For example::
|
||||||
__metaclass__ = models.SubfieldBase
|
__metaclass__ = models.SubfieldBase
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
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
|
This ensures that the :meth:`.to_python` method, documented below, will always
|
||||||
be called when the attribute is initialized.
|
be called when the attribute is initialized.
|
||||||
|
|
Loading…
Reference in New Issue