REST Framework¶
Enhancements for the Django REST Framework, mainly to support DRY development, as the REST framework by default requires a lot of boilerplate code w/ all the routers, serialisers & viewsets.
REST Authentication¶
Custom authentications for the Django REST Framework.
- class confirm.django.rest_framework.authentication.CsrfExemptSessionAuthentication¶
The
rest_framework.authentication.SessionAuthentication
class, but with disabled CSRF protection.- enforce_csrf(request)¶
Do not enforce CSRF validation for session based authentication.
- Parameters:
request (rest_framework.request.Request) – The request object
REST Permissions¶
Custom permissions for the Django REST Framework.
- class confirm.django.rest_framework.permissions.IsAuthenticatedReadOnly¶
Permission class which only allows authenticated users, and it only allows them to access safe methods.
- has_permission(request, view)¶
Checks the permissions.
- Parameters:
request (rest_framework.request.Request) – The request object
view (mixed) – The view
- Returns:
True
if permission granted,False
if not- Return type:
bool
REST Serializers¶
Custom serializers for the Django REST Framework.
- class confirm.django.rest_framework.serializers.NestedPrimaryKeyRelatedField(*args, **kwargs)¶
A primary key related field but on steroids.
- Parameters:
args (list) – The args to pass to the
PrimaryKeyRelatedField
kwargs (dict) – The kwargs to pass to the
PrimaryKeyRelatedField
Hint
This is field is required as a workaround for a design flaw in the Django REST framework. Have a look at the
SimpleModelSerializer.build_nested_field()
method for more informations.Instead of using the primitive data type for the representation, this field accepts a
rest_framework.serializers.Serializer
and uses that to render the nested representation data.- get_choices(cutoff=None)¶
The choices for the browsable API view.
- Parameters:
cutoff (int) – The cutoff for the queryset
- Returns:
All choices
- Return type:
collections.OrderedDict
- to_representation(value)¶
Instead of transforming the outgoing native value into primitive data, use the serializer for the data representation.
- Parameters:
value (mixed) – The instance
- Returns:
The representation value
- Return type:
mixed
- use_pk_only_optimization()¶
Set flag to return a mock object only containing the pk attribute in case the
self.serializer
is set.- Returns:
Flag for the mock object
- Return type:
bool
- class confirm.django.rest_framework.serializers.SimpleHyperlinkedModelSerializer(*args, **kwargs)¶
A
rest_framework.serializers.HyperlinkedModelSerializer
with a bit of magic, inherited from theSimpleModelSerializer
.- build_nested_field(field_name, relation_info, nested_depth)¶
Create a nested field serializer for a forward or reverse relationship.
- Parameters:
field_name (str) – The name of the field
relation_info (rest_framework.utils.model_meta.RelationInfo) – The relation info
nested_depth (int) – The depth of the nested field
- Returns:
The field class and field kwargs
- Return type:
tuple (class, dict)
- to_internal_value(data)¶
Convert primitive data value to native value.
- Parameters:
data (mixed) – The primitive data value
- Returns:
The native value
- Return type:
mixed
- class confirm.django.rest_framework.serializers.SimpleModelSerializer(*args, **kwargs)¶
A
rest_framework.serializers.ModelSerializer
with a bit of magic.While the original serializer requires a defined
Meta
class with all the informations, this serializer can live without it. In case theMeta
is absent, the serializer will automatically create one.- Parameters:
args (list) – The args to pass to the
ModelSerializer
model (django.db.models.Model) – The model
fields (list or str) – The fields
exclude (None or list) – The exclude fields
depth (int) – The nested depth
kwargs (dict) – The kwargs to pass to the
ModelSerializer
- build_nested_field(field_name, relation_info, nested_depth)¶
Create nested fields for forward and reverse relationships.
Hint
We’re overwriting this method due to a design flaw in the Django REST framework, when using nested serializers, i.e. when the
self.depth
parameter is used. While read-only requests (i.e.GET
) work just fine, write requests (e.g.POST
) will fail because they are treated read-only now. With this overloading, write actions should work again.This is achieved by using our modified
NestedPrimaryKeyRelatedField
field in casenested_depth
is greater than0
.- Parameters:
field_name (str) – The name of the field
relation_info (rest_framework.utils.model_meta.RelationInfo) – The relation info
nested_depth (int) – The nested depth
- Returns:
The field class and field kwargs
- Return type:
tuple (class, dict)
- build_relational_field(field_name, relation_info)¶
Create a field for forward and/or reverse relationships.
In case there’s a
view_name
in the returned field kwargs, it will automatically be namespaced via thenamespace_view_name()
method.- Parameters:
field_name (str) – The name of the field
relation_info (rest_framework.utils.model_meta.RelationInfo) – The relation info
- Returns:
The field class and field kwargs
- Return type:
tuple (class, dict)
- build_url_field(field_name, model_class)¶
Create a field representing the object’s own URL.
In case there’s a
view_name
in the returned field kwargs, it will automatically be namespaced via thenamespace_view_name()
method.- Parameters:
field_name (str) – The name of the URL field
model_class (django.db.models.Model) – The django database model class
- Returns:
The field class and field kwargs
- Return type:
tuple (class, dict)
- classmethod namespace_view_name(kwargs)¶
In case the kwargs include a view_name item, the view_name item will be namepsaced with the the class’ namespace property.
- Parameters:
kwargs (dict) – The field kwargs
- Returns:
The keyword arguments but with a namespaced view_name
- Return type:
dict