Python rest_framework.settings.api_settings.URL_FIELD_NAME Examples

The following are 7 code examples of rest_framework.settings.api_settings.URL_FIELD_NAME(). 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 also want to check out all available functions/classes of the module rest_framework.settings.api_settings , or try the search function .
Example #1
Source File: renderers.py    From django-rest-framework-json-api with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def build_json_resource_obj(cls, fields, resource, resource_instance, resource_name,
                                force_type_resolution=False):
        """
        Builds the resource object (type, id, attributes) and extracts relationships.
        """
        # Determine type from the instance if the underlying model is polymorphic
        if force_type_resolution:
            resource_name = utils.get_resource_type_from_instance(resource_instance)
        resource_data = [
            ('type', resource_name),
            ('id', encoding.force_str(resource_instance.pk) if resource_instance else None),
            ('attributes', cls.extract_attributes(fields, resource)),
        ]
        relationships = cls.extract_relationships(fields, resource, resource_instance)
        if relationships:
            resource_data.append(('relationships', relationships))
        # Add 'self' link if field is present and valid
        if api_settings.URL_FIELD_NAME in resource and \
                isinstance(fields[api_settings.URL_FIELD_NAME], relations.RelatedField):
            resource_data.append(('links', {'self': resource[api_settings.URL_FIELD_NAME]}))
        return OrderedDict(resource_data) 
Example #2
Source File: metadata.py    From django-rest-framework-json-api with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_serializer_info(self, serializer):
        """
        Given an instance of a serializer, return a dictionary of metadata
        about its fields.
        """
        if hasattr(serializer, 'child'):
            # If this is a `ListSerializer` then we want to examine the
            # underlying child serializer instance instead.
            serializer = serializer.child

        # Remove the URL field if present
        serializer.fields.pop(api_settings.URL_FIELD_NAME, None)

        return OrderedDict([
            (format_value(field_name), self.get_field_info(field))
            for field_name, field in serializer.fields.items()
        ]) 
Example #3
Source File: serializers.py    From gro-api with GNU General Public License v2.0 5 votes vote down vote up
def get_default_field_names(self, declared_fields, model_info):
        return (
            [api_settings.URL_FIELD_NAME] +
            list(declared_fields.keys()) +
            list(model_info.fields.keys()) +
            list(model_info.forward_relations.keys()) +
            list(model_info.reverse_relations.keys())
        ) 
Example #4
Source File: serializers.py    From gro-api with GNU General Public License v2.0 5 votes vote down vote up
def build_field(self, field_name, info, model_class, nested_depth):
        if field_name in info.fields_and_pk:
            model_field = info.fields_and_pk[field_name]
            return self.build_standard_field(field_name, model_field)
        elif field_name in info.relations:
            relation_info = info.relations[field_name]
            if not nested_depth:
                field_class, field_kwargs = self.build_relational_field(
                    field_name, relation_info
                )
            else:
                field_class, field_kwargs = self.build_nested_field(
                    field_name, relation_info, nested_depth
                )
            # TODO: Make sure this actually does something
            # Don't allow writes to relations resulting from foreign keys
            # pointing to this class
            if relation_info.model_field is None:
                field_kwargs['read_only'] = True
                if 'queryset' in field_kwargs:
                    field_kwargs.pop('queryset')
            return field_class, field_kwargs
        elif hasattr(model_class, field_name):
            return self.build_property_field(field_name, model_class)
        elif field_name == api_settings.URL_FIELD_NAME:
            return self.build_url_field(field_name, model_class)
        return self.build_unknown_field(field_name, model_class) 
Example #5
Source File: mixins.py    From Dailyfresh-B2C with Apache License 2.0 5 votes vote down vote up
def get_success_headers(self, data):
        try:
            return {'Location': str(data[api_settings.URL_FIELD_NAME])}
        except (TypeError, KeyError):
            return {} 
Example #6
Source File: views.py    From website with MIT License 5 votes vote down vote up
def get_success_headers(self, data):
        try:
            return {'Location': str(data[api_settings.URL_FIELD_NAME])}
        except (TypeError, KeyError):
            return {} 
Example #7
Source File: renderers.py    From drf-json-api with MIT License 4 votes vote down vote up
def handle_nested_serializer(self, resource, field, field_name, request):
        serializer_field = get_related_field(field)

        if hasattr(serializer_field, "opts"):
            model = serializer_field.opts.model
        else:
            model = serializer_field.Meta.model

        resource_type = self.model_to_resource_type(model)

        linked_ids = self.dict_class()
        links = self.dict_class()
        linked = self.dict_class()
        linked[resource_type] = []

        if is_related_many(field):
            items = resource[field_name]
        else:
            items = [resource[field_name]]

        obj_ids = []

        resource.serializer = serializer_field

        for item in items:
            converted = self.convert_resource(item, resource, request)
            linked_obj = converted["data"]
            linked_ids = converted.pop("linked_ids", {})

            if linked_ids:
                linked_obj["links"] = linked_ids

            obj_ids.append(converted["data"]["id"])

            field_links = self.prepend_links_with_name(
                converted.get("links", {}), resource_type)

            field_links[field_name] = {
                "type": resource_type,
            }

            if "href" in converted["data"]:
                url_field_name = api_settings.URL_FIELD_NAME
                url_field = serializer_field.fields[url_field_name]

                field_links[field_name]["href"] = self.url_to_template(
                    url_field.view_name, request, field_name,
                )

            links.update(field_links)

            linked[resource_type].append(linked_obj)

        if is_related_many(field):
            linked_ids[field_name] = obj_ids
        else:
            linked_ids[field_name] = obj_ids[0]

        return {"linked_ids": linked_ids, "links": links, "linked": linked}