resource

class resources.BaseResource(view=None, depth=None, stack=[], **kwargs)[source]

Base class for all Resource classes, which simply defines the interface they provide.

validate_request(data, files=None)[source]

Given the request content return the cleaned, validated content. Typically raises a response.ErrorResponse with status code 400 (Bad Request) on failure.

filter_response(obj)[source]

Given the response content, filter it into a serializable object.

class resources.Resource(view=None, depth=None, stack=[], **kwargs)[source]

A Resource determines how a python object maps to some serializable data. Objects that a resource can act on include plain Python object instances, Django Models, and Django QuerySets.

class resources.FormResource(view=None, depth=None, stack=[], **kwargs)[source]

Resource class that uses forms for validation. Also provides a get_bound_form() method which may be used by some renderers.

On calling validate_request() this validator may set a bound_form_instance attribute on the view, which may be used by some renderers.

form = None

The Form class that should be used for request validation. This can be overridden by a form attribute on the views.View.

validate_request(data, files=None)[source]

Given some content as input return some cleaned, validated content. Raises a response.ErrorResponse with status code 400 (Bad Request) on failure.

Validation is standard form validation, with an additional constraint that no extra unknown fields may be supplied.

On failure the response.ErrorResponse content is a dict which may contain 'errors' and 'field-errors' keys. If the 'errors' key exists it is a list of strings of non-field errors. If the 'field-errors' key exists it is a dict of {'field name as string': ['errors as strings', ...]}.

get_form_class(method=None)[source]

Returns the form class used to validate this resource.

get_bound_form(data=None, files=None, method=None)[source]

Given some content return a Django form bound to that content. If form validation is turned off (form class attribute is None) then returns None.

class resources.ModelResource(view=None, depth=None, stack=[], **kwargs)[source]

Resource class that uses forms for validation and otherwise falls back to a model form if no form is set. Also provides a get_bound_form() method which may be used by some renderers.

form = None

The form class that should be used for request validation. If set to None then the default model form validation will be used.

This can be overridden by a form attribute on the views.View.

fields = None

The list of fields to use on the output.

May be any of:

The name of a model field. To view nested resources, give the field as a tuple of (“fieldName”, resource) where resource may be any of ModelResource reference, the name of a ModelResourc reference as a string or a tuple of strings representing fields on the nested model. The name of an attribute on the model. The name of an attribute on the resource. The name of a method on the model, with a signature like func(self). The name of a method on the resource, with a signature like func(self, instance).

exclude = ('id', 'pk')

The list of fields to exclude. This is only used if fields is not set.

include = ('url',)

The list of extra fields to include. This is only used if fields is not set.

model = None

The model class which this resource maps to.

This can be overridden by a model attribute on the views.View.

validate_request(data, files=None)[source]

Given some content as input return some cleaned, validated content. Raises a response.ErrorResponse with status code 400 (Bad Request) on failure.

Validation is standard form or model form validation, with an additional constraint that no extra unknown fields may be supplied, and that all fields specified by the fields class attribute must be supplied, even if they are not validated by the form/model form.

On failure the ErrorResponse content is a dict which may contain 'errors' and 'field-errors' keys. If the 'errors' key exists it is a list of strings of non-field errors. If the ‘’field-errors’` key exists it is a dict of {field name as string: list of errors as strings}.

get_bound_form(data=None, files=None, method=None)[source]

Given some content return a Form instance bound to that content.

If the form class attribute has been explicitly set then that class will be used to create the Form, otherwise the model will be used to create a ModelForm.

url(instance)[source]

Attempts to reverse resolve the url of the given model instance for this resource.

Requires a View with mixins.InstanceMixin to have been created for this resource.

This method can be overridden if you need to set the resource url reversing explicitly.

Project Versions

Previous topic

renderers

Next topic

response

This Page