scrapy/docs/experimental/ref/newitem/fields.rst

3.5 KiB

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> </head>

Item Fields

System Message: ERROR/3 (<stdin>, line 7)

Unknown directive type "module".

.. module:: scrapy.newitem.fields

Field options

Every Field class constructor accepts these arguments.

default

The default value for the field. See :ref:`topics-newitem-index-defaults`.

System Message: ERROR/3 (<stdin>, line 17); backlink

Unknown interpreted text role "ref".

Field types

These are the available built-in Field types. See :ref:`ref-newitem-fields-custom-fields` for info on creating your own field types.

System Message: ERROR/3 (<stdin>, line 22); backlink

Unknown interpreted text role "ref".

TextField

A unicode text.

IntegerField

An integer.

DecimalField

A fixed-precision decimal number, represented in Python by a Decimal instance.

FloatField

A floating-point number represented in Python by a float instance.

BooleanField

A boolean (true/false) field.

DateTimeField

A date with time, represented in Python by a datetime.datetime instance.

DateField

A date, represented in Python by a datetime.date instance.

TimeField

A time, represented in Python by a datetime.time instance.

Creating custom fields

All field classes are subclasses of the :class:`BaseField` class (see below) which you can also subclass to create your own custom fields.

System Message: ERROR/3 (<stdin>, line 95); backlink

Unknown interpreted text role "class".

You can also subclass a more specific field class, say :class:`DecimalField`, to implement a PriceField, for example.

System Message: ERROR/3 (<stdin>, line 98); backlink

Unknown interpreted text role "class".

BaseField class

The base class for all fields. It only provides code for handling default values, not any particular type. It cannot be used directly either, as its :meth:`BaseField.to_python` method is not implemented.

System Message: ERROR/3 (<stdin>, line 106); backlink

Unknown interpreted text role "meth".

The default argument (if given) must be of the type expected by this field, or any type that is accepted by the :meth:`BaseField.to_python` method of this field.

System Message: ERROR/3 (<stdin>, line 110); backlink

Unknown interpreted text role "meth".

For example:

class NewsItem(Item):
    content = fields.TextField() # correct, no default value
    author = fields.TextField(default=u'Myself") # correct, with default value
    published = fields.DateField(default=23) # wrong default type (will raise TypeError)

System Message: ERROR/3 (<stdin>, line 121)

Unknown directive type "method".

.. method:: to_python(value)

   Convert the input value to the type expected by this field and return
   it.

   For example, :class:`IntegerField` would convert ``'1'`` to ``1``, while
   :class:`DecimalField` would convert ``'1'`` to ``Decimal('1')`` and so
   on.

   This method is not implemented in the :class:`BaseField` class, so it
   must always be implemented in all its subclasses, in order to be usable.

   This method should raise ``TypeError`` if the input type is not
   supported, and ``ValueError`` if the input type is support but its value
   is not appropriate (for example, an integer outside a given range).

   This method must always return object of the expected field type.

System Message: ERROR/3 (<stdin>, line 139)

Unknown directive type "method".

.. method:: get_default()

   Return the default value for this field, or ``None`` if the field
   doesn't specify any.
</html>