A package supplying tools for custom foreign-key fields lookup in drf-created WebAPI.
Project description
Lookup Fields
This package provides utils for changing lookup fields in requests to Django Rest Framework endpoints. It supports choosing lookup field for identifier of endpoint and identifiers used in related fields of serializers.
Per field lookup usage
Data for field should be supplied in Content-Lookup-Fields
HTTP header as base64 encoded json object where keys are serializer fields names (if nested use dotted notation) and values are intended lookup fields:
{
"some_field": "uuid",
"other.nested.field": "pk",
}
Field
from lookup_fields.fields import CustomizableLookupRelatedField
from rest_framework import serializers
class Serializer(serializers.ModelSerializer):
field = CustomizableLookupRelatedField(
lookup_fields={
'some_field': serializers.CharField(),
'uuid': serializers.UUIDField(),
'other_field': serializers.IntegerField(),
'another_field_with_custom_filter_lookup_suffix': {
'field': serializers.CharField(max_length=20),
'filter_lookup_suffix': '__icontains',
}
},
no_pk_lookup=True,
default_lookup_field_name='uuid',
)
default_lookup_field_name is 'pk', integer pk field is always added to lookup_fields. If you don't want this behavior set no_pk_lookup to True. Even then you can still add pk to lookup_fields when instantiate field.
You can specify optional parameters to lookup_fields by passing a dict instead of Field instance. Field has to be passed under 'field' key.
Serializer
Serializers uses CustomizableLookupRelatedField
as default related field.
from lookup_fields.fields import CustomizableLookupRelatedField
from rest_framework import serializers
class Serializer(CustomizableLookupRelatedField):
class Meta:
model = SomeModel
fields = (
'relation',
)
extra_kwargs = {
'relation': {
'lookup_fields': {
'some_field': serializers.CharField(),
},
},
}
View mixin usage
Name of lookup field should be supplied in Lookup-Field
HTTP header.
View
from lookup_fields.views import CustomizableLookupFieldMixin
from rest_framework.generics import GenericAPIView
class View(CustomizableLookupFieldMixin, GenericAPIView):
ALLOWED_LOOKUP_FIELDS = [
'pk',
'uuid',
'some_field',
WithPermissions(
'other_field',
permissions=['some_app.can_use_other_field_as_lookup_field'],
)
]
LOOKUP_FIELD_VALIDATORS = {
'some_field': [validator],
}
License
The Django Rest Framework Lookup Fields package is licensed under the FreeBSD License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for drf_lookup_fields-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d579a1ea3933101615645a849450fd5b72e8cc60614e29510a63e713b476ca35 |
|
MD5 | a57c063b2b1525769ccb31242b3427af |
|
BLAKE2b-256 | 3e4dbf94c7955bdcbc9b710abeb6393bf8b14259426799892b6aa97677e0ce58 |