Ticket #19539: 19539.diff

File 19539.diff, 1.2 KB (added by Tim Graham, 11 years ago)
  • docs/howto/custom-model-fields.txt

    diff --git a/docs/howto/custom-model-fields.txt b/docs/howto/custom-model-fields.txt
    index 54913a8..85c7100 100644
    a b appropriate Python object. The details of how this happens internally are a  
    249249little complex, but the code you need to write in your ``Field`` class is
    250250simple: make sure your field subclass uses a special metaclass:
    251251
    252 For example::
     252For example, on Python 2::
    253253
    254254    class HandField(models.Field):
    255255
    For example::  
    258258        __metaclass__ = models.SubfieldBase
    259259
    260260        def __init__(self, *args, **kwargs):
    261             # ...
     261            ...
     262
     263On Python 3, in lieu of setting the ``__metaclass__`` attribute, add
     264``metaclass`` to the class definition::
     265
     266    class HandField(models.Field, metaclass=models.SubfieldBase):
     267        ...
     268
     269If you want your code to work on Python 2 & 3, you can use
     270:func:`six.with_metaclass`::
     271
     272    from django.utils.six import with_metaclass
     273
     274    class HandField(with_metaclass(models.SubfieldBase, models.Field)):
     275        ...
    262276
    263277This ensures that the :meth:`.to_python` method, documented below, will always
    264278be called when the attribute is initialized.
Back to Top