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/?lookup_action=create&lookup_field=category
GET /articles/lookup_filterset/?lookup_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.1.tar.gz
(9.9 kB
view details)
Built Distribution
File details
Details for the file drf_lookup-0.1.1.tar.gz
.
File metadata
- Download URL: drf_lookup-0.1.1.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c089fe0347c5756113f21c5740c9f01b0105fc531a4d9eb7e138952af9c77a8a |
|
MD5 | 249afb4bf583d842cb254e51c78585b3 |
|
BLAKE2b-256 | 3e8bb008f488a3edbbc199316212f19b11745e4b1e0466e54bb6f3105719c054 |
File details
Details for the file drf_lookup-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: drf_lookup-0.1.1-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e5c4fbf294b93c6b60cf5b7001943f5184b42be18d666adc734d6c34bd1c287 |
|
MD5 | 5f476e8a9043084b5cff8f4ac9f0186c |
|
BLAKE2b-256 | ef13750695330d6477eb12728cf6c0342638b41d9d99577eb004a4ac5e2d1f80 |