Skip to main content

A package supplying tools for custom foreign-key fields lookup in drf-created WebAPI.

Project description

Lookup Fields

Test package Coverage Status

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf-lookup-fields-1.1.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_lookup_fields-1.1.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file drf-lookup-fields-1.1.0.tar.gz.

File metadata

  • Download URL: drf-lookup-fields-1.1.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.12 Linux/5.13.0-1017-azure

File hashes

Hashes for drf-lookup-fields-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ca2f1af211f8732efba137452289af004602285abd8da89c02263c162b558a42
MD5 4712534355828b32feaefc114bf49f98
BLAKE2b-256 2a145745910803f048886be2c68a8937fe47f54d57f87c4bf0a2899e4a7bf220

See more details on using hashes here.

File details

Details for the file drf_lookup_fields-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: drf_lookup_fields-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.12 Linux/5.13.0-1017-azure

File hashes

Hashes for drf_lookup_fields-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d579a1ea3933101615645a849450fd5b72e8cc60614e29510a63e713b476ca35
MD5 a57c063b2b1525769ccb31242b3427af
BLAKE2b-256 3e4dbf94c7955bdcbc9b710abeb6393bf8b14259426799892b6aa97677e0ce58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page