Skip to main content

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

Project description

Drf-lookup

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.0.tar.gz (46.2 kB view details)

Uploaded Source

Built Distribution

drf_lookup-1.0.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for drf_lookup-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c03f436d8bebe83dd3a319fbd0856906a33eb7cb3a2c3fb852f84f414d4da010
MD5 16491afa48d066ed77bcbaf3d8c7b76b
BLAKE2b-256 ed85fe587be54cb1fbca72a7f1039e56b40d619fc9945fd2e5150bda50f36243

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_lookup-1.0.0.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.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for drf_lookup-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9cfcddb67b0bc31c1689cc08433d1858e0867210bee063e050300f2063f1f2a
MD5 b8c0b1004486ce2a96cb38647639b720
BLAKE2b-256 46fde12b1b2663b1449b34e68b2a80af482065aa32602abbbe04e3f4d1d64d63

See more details on using hashes here.

Provenance

The following attestation bundles were made for drf_lookup-1.0.0-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 Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page