Python rest_framework.serializers.ManyRelatedField() Examples

The following are 7 code examples for showing how to use rest_framework.serializers.ManyRelatedField(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module rest_framework.serializers , or try the search function .

Example 1
Project: drf-schema-adapter   Author: drf-forms   File: get_field_dict.py    License: MIT License 6 votes vote down vote up
def update_relationship_from_serializer(self, rv, field_instance, foreign_key_as_list):
        if not isinstance(field_instance, (relations.PrimaryKeyRelatedField, relations.ManyRelatedField,
                                           relations.SlugRelatedField)):
            return

        if not hasattr(field_instance, 'queryset') or field_instance.queryset is None:
            return

        related_model = field_instance.queryset.model

        if not foreign_key_as_list:
            self.update_related_endpoint(rv, related_model)
        else:
            if not hasattr(field_instance, 'queryset') or field_instance.queryset is None:
                return
            # FIXME: we may not need this code as the serializer field has a 'choices' attribute
            self.set_choices_from_qs(rv, field_instance.queryset) 
Example 2
Project: drf-json-api   Author: kevin-brown   File: utils.py    License: MIT License 5 votes vote down vote up
def get_related_field(field):
    if isinstance(field, ManyRelatedField):
        return field.child_relation

    if isinstance(field, ListSerializer):
        return field.child

    return field 
Example 3
Project: drf-json-api   Author: kevin-brown   File: utils.py    License: MIT License 5 votes vote down vote up
def is_related_many(field):
    if hasattr(field, "many"):
        return field.many

    if isinstance(field, ManyRelatedField):
        return True

    if isinstance(field, ListSerializer):
        return True

    return False 
Example 4
Project: ChRIS_ultron_backEnd   Author: FNNDSC   File: renderers.py    License: MIT License 5 votes vote down vote up
def _get_related_fields(self, fields, id_field):
        return [k for (k, v) in fields
                if k != id_field
                and (isinstance(v, HyperlinkedRelatedField)
                or isinstance(v, HyperlinkedIdentityField)   
                or isinstance(v, ItemLinkField)
                or (isinstance(v, ManyRelatedField)
                    and isinstance(v.child_relation, HyperlinkedRelatedField)))] 
Example 5
Project: Dailyfresh-B2C   Author: BeanWei   File: metadata.py    License: Apache License 2.0 5 votes vote down vote up
def get_field_info(self, field):
        """
        Given an instance of a serializer field, return a dictionary
        of metadata about it.
        """
        field_info = OrderedDict()
        field_info['type'] = self.label_lookup[field]
        field_info['required'] = getattr(field, 'required', False)

        attrs = [
            'read_only', 'label', 'help_text',
            'min_length', 'max_length',
            'min_value', 'max_value'
        ]

        for attr in attrs:
            value = getattr(field, attr, None)
            if value is not None and value != '':
                field_info[attr] = force_text(value, strings_only=True)

        if getattr(field, 'child', None):
            field_info['child'] = self.get_field_info(field.child)
        elif getattr(field, 'fields', None):
            field_info['children'] = self.get_serializer_info(field)

        if (not field_info.get('read_only') and
            not isinstance(field, (serializers.RelatedField, serializers.ManyRelatedField)) and
                hasattr(field, 'choices')):
            field_info['choices'] = [
                {
                    'value': choice_value,
                    'display_name': force_text(choice_name, strings_only=True)
                }
                for choice_value, choice_name in field.choices.items()
            ]

        return field_info 
Example 6
Project: drf-schema-adapter   Author: drf-forms   File: get_field_dict.py    License: MIT License 5 votes vote down vote up
def get_read_only(self, name, field_instance):
        if name == '__str__':
            return True

        if field_instance.read_only and not isinstance(field_instance, serializers.ManyRelatedField):
            return True

        return False 
Example 7
Project: django-rest-framework-json-api   Author: django-json-api   File: metadata.py    License: BSD 2-Clause "Simplified" License 4 votes vote down vote up
def get_field_info(self, field):
        """
        Given an instance of a serializer field, return a dictionary
        of metadata about it.
        """
        field_info = OrderedDict()
        serializer = field.parent

        if isinstance(field, serializers.ManyRelatedField):
            field_info['type'] = self.type_lookup[field.child_relation]
        else:
            field_info['type'] = self.type_lookup[field]

        try:
            serializer_model = getattr(serializer.Meta, 'model')
            field_info['relationship_type'] = self.relation_type_lookup[
                getattr(serializer_model, field.field_name)
            ]
        except KeyError:
            pass
        except AttributeError:
            pass
        else:
            field_info['relationship_resource'] = get_related_resource_type(field)

        field_info['required'] = getattr(field, 'required', False)

        attrs = [
            'read_only', 'write_only', 'label', 'help_text',
            'min_length', 'max_length',
            'min_value', 'max_value', 'initial'
        ]

        for attr in attrs:
            value = getattr(field, attr, None)
            if value is not None and value != '':
                field_info[attr] = force_str(value, strings_only=True)

        if getattr(field, 'child', None):
            field_info['child'] = self.get_field_info(field.child)
        elif getattr(field, 'fields', None):
            field_info['children'] = self.get_serializer_info(field)

        if (
            not field_info.get('read_only') and
            not field_info.get('relationship_resource') and
            hasattr(field, 'choices')
        ):
            field_info['choices'] = [
                {
                    'value': choice_value,
                    'display_name': force_str(choice_name, strings_only=True)
                }
                for choice_value, choice_name in field.choices.items()
            ]

        if hasattr(serializer, 'included_serializers') and 'relationship_resource' in field_info:
            field_info['allows_include'] = field.field_name in serializer.included_serializers

        return field_info