class Field

from rest_framework.fields import Field
Documentation Source code

Ancestors (MRO)

  1. Field

Attributes

  Defined in
_use_files = None Field
creation_counter = 4 Field
empty = Field
form_field_class = <class 'django.forms.fields.CharField'> Field
read_only = True Field
type_name = None Field
Expand Collapse

Methods

def attributes(self): Field

        Returns a dictionary of attributes to be used when serializing to xml.
        
    def attributes(self):
        """
        Returns a dictionary of attributes to be used when serializing to xml.
        """
        if self.type_name:
            return {'type': self.type_name}
        return {}

def field_from_native(self, data, files, field_name, into): Field

        Given a dictionary and a field name, updates the dictionary `into`,
        with the field and it's deserialized value.
        
    def field_from_native(self, data, files, field_name, into):
        """
        Given a dictionary and a field name, updates the dictionary `into`,
        with the field and it's deserialized value.
        """
        return

def field_to_native(self, obj, field_name): Field

        Given and object and a field name, returns the value that should be
        serialized for that field.
        
    def field_to_native(self, obj, field_name):
        """
        Given and object and a field name, returns the value that should be
        serialized for that field.
        """
        if obj is None:
            return self.empty

        if self.source == '*':
            return self.to_native(obj)

        if self.source:
            value = obj
            for component in self.source.split('.'):
                value = getattr(value, component)
                if is_simple_callable(value):
                    value = value()
        else:
            value = getattr(obj, field_name)
        return self.to_native(value)

def initialize(self, parent, field_name): Field

        Called to set up a field prior to field_to_native or field_from_native.

        parent - The parent serializer.
        model_field - The model field this field corresponds to, if one exists.
        
    def initialize(self, parent, field_name):
        """
        Called to set up a field prior to field_to_native or field_from_native.

        parent - The parent serializer.
        model_field - The model field this field corresponds to, if one exists.
        """
        self.parent = parent
        self.root = parent.root or parent
        self.context = self.root.context
        if self.root.partial:
            self.required = False

def to_native(self, value): Field

        Converts the field's value into it's simple representation.
        
    def to_native(self, value):
        """
        Converts the field's value into it's simple representation.
        """
        if is_simple_callable(value):
            value = value()

        if is_protected_type(value):
            return value
        elif hasattr(value, '__iter__') and not isinstance(value, (dict, basestring)):
            return [self.to_native(item) for item in value]
        elif isinstance(value, dict):
            return dict(map(self.to_native, (k, v)) for k, v in value.items())
        return smart_unicode(value)