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.

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)

    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


# serializers
from rest_framework.serializers import ModelSerializer
class ArticleSerializer(ModelSerializer):
    class Meta:
        model = Article
        fields = ('id', 'title', 'category')


# 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, we can request options for the category field:

GET /articles/lookup_serializer/?action=create&lookup_field=category

GET /articles/lookup_filterset/?action=list&lookup_field=category

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

Uploaded Source

Built Distribution

drf_lookup-0.1.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: drf_lookup-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for drf_lookup-0.1.0.tar.gz
Algorithm Hash digest
SHA256 31648d26dc838b1610915be2cbc4f0dbd859bac0fd2d7907cd5f30c19a894a81
MD5 36f9f2c656ba083c134b3c8124e150de
BLAKE2b-256 300a93e3e3198809932e7d6222fcf4a52f999b2353bdc85ceccc455ea0efd617

See more details on using hashes here.

File details

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

File metadata

  • Download URL: drf_lookup-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for drf_lookup-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de97322682b5690375b500ed8734d958c32b51f9cc7454a46c90b18bd299f73b
MD5 e03e2ce0e23453410ea0687a28894224
BLAKE2b-256 7bc758eeeacec6a635c8c7bc6f6f1fe3575ea5b824e40a873bd3c2336705c3bb

See more details on using hashes here.

Supported by

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