Skip to main content

Helper for retrieve select options of drf fields and django-filter filters.

Project description

Drf-lookup

PyPI - Version Tests Codecov

Drf-lookup helps you retrieve options for serializer fields and django-filter filters. It adds additional actions to the viewset, checks the queryset and choices attributes and returns valid values for the requested field/filter. This is useful when you are retrieving parameters asynchronously and don't need to create a view for each case.

Install

pip install drf-lookup

Example

# models
from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=100, db_index=True)
    is_public = models.BooleanField(default=True)

    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'
        ordering = ['name']

    def __str__(self) -> str:
        return self.name


class Article(models.Model):
    title = models.CharField(max_length=100, db_index=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)

    class Meta:
        verbose_name = 'news'
        verbose_name_plural = 'news'
        ordering = ['-pk']

    def __str__(self) -> str:
        return self.title


# Create some objects
category = Category.objects.create(name='Category 1')
Category.objects.create(name='Category 2', is_public=False)
Category.objects.create(name='Category 3')
Article.objects.create(title='Test title', category=category)


# serializers
from rest_framework.serializers import ModelSerializer

class ArticleSerializer(ModelSerializer):
    class Meta:
        model = Article
        fields = ('id', 'title', 'category')

        # Limit categories by the `is_public` attribute
        extra_kwargs = {
            'category': {'queryset': Category.objects.filter(is_public=True)},
        }

# filters
import django_filters

class ArticleFilterSet(django_filters.FilterSet):
    class Meta:
        model = Article
        fields = ('category',)


# views
from rest_framework.viewsets import ModelViewSet

from drf_lookup.views import LookupMixin


class ArticleViewSet(LookupMixin, ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
    filterset_class = ArticleFilterSet

Now, it's possible to retrieve valid options for the serializer's category field.

// GET /api/articles/lookup_serializer/?lookup_action=create&lookup_field=category
// Only public categories will be returned

[
  {
    "id": 1,
    "name": "Category 1"
  },
  {
    "id": 3,
    "name": "Category 3"
  }
]

And for the filter's category field.

// GET /api/articles/lookup_filter/?lookup_action=list&lookup_field=category
// Only categories specified in articles will be returned

[
  {
    "id": 1,
    "name": "Category 1"
  },
]

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-1.0.1.tar.gz (52.9 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-1.0.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file drf_lookup-1.0.1.tar.gz.

File metadata

  • Download URL: drf_lookup-1.0.1.tar.gz
  • Upload date:
  • Size: 52.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for drf_lookup-1.0.1.tar.gz
Algorithm Hash digest
SHA256 57f3f58ba2d65a13e224864c69f423934eb850c954ff87bfe3c1f241d01f11bd
MD5 7aba2907dab1a8f56047cc005008afdd
BLAKE2b-256 b59125fd4c8f86c39f4772d3837e59fcaa9baee28ef894088a29f7c3f279b8a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_lookup-1.0.1.tar.gz:

Publisher: release.yml on kindlycat/drf-lookup

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file drf_lookup-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: drf_lookup-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for drf_lookup-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4e191cdb72fd58b8bc0d7b811098b1db3ef1c7a840d01c669df597162070e414
MD5 93853f51913c0820c558e562080a53d0
BLAKE2b-256 deaf4bb75380b11b7ec9132c94ade151faa910f0d9756993fa898989935b6af7

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_lookup-1.0.1-py3-none-any.whl:

Publisher: release.yml on kindlycat/drf-lookup

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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