# 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
```bash
pip install drf-lookup
```
## Example
```python
# 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
```
Raw data
{
"_id": null,
"home_page": "https://github.com/kindlycat/drf-lookup",
"name": "drf-lookup",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "drf, django-filter",
"author": "Grigory Mishchenko",
"author_email": "grishkokot@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/86/21/7396cc3150d030c801536ef4fb175332bb1bf2a86174487f57999169f2cb/drf_lookup-0.1.6.tar.gz",
"platform": null,
"description": "# Drf-lookup\n\nDrf-lookup helps you retrieve options for serializer fields and django-filter\nfilters. It adds additional actions to the viewset, checks the ``queryset`` and\n``choices`` attributes and returns valid values for the requested field/filter. \nThis is useful when you are retrieving parameters asynchronously and don't need \nto create a view for each case.\n\n## Install\n\n```bash\npip install drf-lookup\n```\n\n## Example\n\n```python\n\n# models\nfrom django.db import models\n\nclass Category(models.Model):\n name = models.CharField(max_length=100, db_index=True)\n\n class Meta:\n verbose_name = 'category'\n verbose_name_plural = 'categories'\n ordering = ['name']\n\n def __str__(self) -> str:\n return self.name\n\n\nclass Article(models.Model):\n title = models.CharField(max_length=100, db_index=True)\n category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)\n\n class Meta:\n verbose_name = 'news'\n verbose_name_plural = 'news'\n ordering = ['-pk']\n\n def __str__(self) -> str:\n return self.title\n\n\n# serializers\nfrom rest_framework.serializers import ModelSerializer\nclass ArticleSerializer(ModelSerializer):\n class Meta:\n model = Article\n fields = ('id', 'title', 'category')\n\n\n# filters\nimport django_filters\nclass ArticleFilterSet(django_filters.FilterSet):\n class Meta:\n model = Article\n fields = ('category',)\n\n\n# views\nfrom rest_framework.viewsets import ModelViewSet\n\nfrom drf_lookup.views import LookupMixin\n\n\nclass ArticleViewSet(LookupMixin, ModelViewSet):\n queryset = Article.objects.all()\n serializer_class = ArticleSerializer\n filterset_class = ArticleFilterSet\n\n```\n\nNow, we can request options for the `category` field:\n\n```\nGET /articles/lookup_serializer/?lookup_action=create&lookup_field=category\n\nGET /articles/lookup_filterset/?lookup_action=list&lookup_field=category\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Helper for retrieve select options of drf fields and django-filter filters.",
"version": "0.1.6",
"project_urls": {
"Homepage": "https://github.com/kindlycat/drf-lookup",
"Repository": "https://github.com/kindlycat/drf-lookup"
},
"split_keywords": [
"drf",
" django-filter"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d5bd18eab8d7cee49fb4f1c820e9d855101264ce72b59cbb6bdd359063603b74",
"md5": "fd355ee63407ffce9507168994c01df8",
"sha256": "fee0a316d89f7f36ce90f726d176f27ff66fae3f98d938f5b3e130a60b05fcac"
},
"downloads": -1,
"filename": "drf_lookup-0.1.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fd355ee63407ffce9507168994c01df8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 13470,
"upload_time": "2024-08-05T08:36:08",
"upload_time_iso_8601": "2024-08-05T08:36:08.007791Z",
"url": "https://files.pythonhosted.org/packages/d5/bd/18eab8d7cee49fb4f1c820e9d855101264ce72b59cbb6bdd359063603b74/drf_lookup-0.1.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "86217396cc3150d030c801536ef4fb175332bb1bf2a86174487f57999169f2cb",
"md5": "99113bba76064ed205ecfae83ba36542",
"sha256": "fd160c106196fc03e0537e7b0237ff4b4dacecc28074ec06b79c0010fd05ba8d"
},
"downloads": -1,
"filename": "drf_lookup-0.1.6.tar.gz",
"has_sig": false,
"md5_digest": "99113bba76064ed205ecfae83ba36542",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 10035,
"upload_time": "2024-08-05T08:36:09",
"upload_time_iso_8601": "2024-08-05T08:36:09.474120Z",
"url": "https://files.pythonhosted.org/packages/86/21/7396cc3150d030c801536ef4fb175332bb1bf2a86174487f57999169f2cb/drf_lookup-0.1.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-05 08:36:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kindlycat",
"github_project": "drf-lookup",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "drf-lookup"
}