Skip to main content

Django Lookup Tables

Efficient storage and management of lookup tables used throughout an app.

Note: This package is a work in progress (that's why it's not yet at version 1.0). I am active seeking contributions to help with making it more usable, see "Contributing" below.

IMPORTANT

This software is still pre-release. Upgrades from one version to the next may create unstabilities in your project. If you have used any version prior to 1.0.0, please read the Release Notes for Beta Versions.

Installation

Install the package:

$ pip install django-lookup-tables

Add it to your installed apps:

INSTALLED_APPS = (
    ...
    'lookup_tables',
    ...
)

Usage

The primary use case for lookup tables is to create user-managed lists of options for models to choose from. Consider a model with a field called, for instance, state:

from django.db import models
from lookup_tables.fields import LookupTableItemField

CHOICES = (('draft', 'draft'), ('published', 'published'))

class Post(models.Model):
    title = models.CharField(max_length=100)
    state = models.CharField(choices=CHOICES)

While this is easy to build, changing the choices list requires rebuilding and redeploying your application.

The above model could instead be written as:

from django.db import models
from lookup_tables.models import AbsractLookupTable
from lookup_tables.fields import LookupField

class PostState(AbstractLookupTable):
    pass

class Post(models.Model):
    title = models.CharField(max_length=100)
    state = LookupField(PostState)

This will create a lookup table called PostState that can be administered by staff users. You can now set this field to any value from the PostState model.

If you register your model in the app's admin.py:

from django.contrib import admin
from lookup_tables.admin import LookupAdmin
from .models import PostState

@admin.register(PostState)
class PostStateAdmin(LookupAdmin):
    pass

... you will be able to modify the values in the table through the "Post State" link in the Django admin.

django-lookup-tables integrates properly with forms out of the box, so all UI naturally gets up-to-date selection lists just like if you were using a CharField with a choices enum or tuple list.

Each table has an arbitrary list of items. You can order them by setting the "Sort Order" field to any positive integer.

Using with Admin-Sortable2

If you have django-admin-sortable2 installed, you can take advantage of it's UI enhancements by configuring django-lookup-tables to use it. In your settings.py:

INSTALLED_APPS = (
    ...
    'adminsortable2',
    'lookup_tables',
    ...
)

LOOKUP_TABLES = {
    'USE_ADMIN_SORTABLE2': True,
}

Using with Django REST Framework

Fields on models will render the same way CharField does if you use the drf_fields.LookupSerializerField field on your serializer like so:

class PostSerializer(serializers.ModelSerializer):

    class Meta:
        fields = ('id', 'title', 'state')

    state = LookupSerializerField(PostState)

By default, the field will send the id of the LookupTableItem. If you instead want to send the name property, add DRF_REPRESENTATION_NAME_NOT_ID to your settings.py:

LOOKUP_TABLES = {
    # ...
    'DRF_REPRESENTATION_NAME_NOT_ID': True,
    # ...
}

The HTML UI provided by DRF will populate dropdowns, and the OPTIONS response handler will supply all key/value pairs available for the field:

OPTIONS /api/posts/1/
HTTP 200 OK
Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "name": "Post Instance",
    "description": "",
    "renders": [
        "application/json",
        "text/html"
    ],
    "parses": [
        "application/json",
        "application/x-www-form-urlencoded",
        "multipart/form-data"
    ],
    "actions": {
        "PUT": {
            "id": {
                "type": "integer",
                "required": false,
                "read_only": true,
                "label": "ID"
            },
            "title": {
                "type": "string",
                "required": true,
                "read_only": false,
                "label": "Name",
                "max_length": 200
            },
            "state": {
                "type": "choice",
                "required": true,
                "read_only": false,
                "label": "State",
                "choices": [
                    {
                        "value": 14,
                        "display_name": "Draft"
                    },
                    {
                        "value": 18,
                        "display_name": "Published"
                    }
                ]
            }
        }
    }
}

Sample App

You can see a sample app using these fields buy running the following:

$ python manage.py migrate
$ python manage.py loaddata fixtures/base.json
$ python manage.py runserver

This app has the following endpoints:

/admin/
/api/mymodel/
/api/mymodel/<id>/

The username for the admin user is admin, and the password is pass.

Contributing

I am actively seeking contributions to this package. Check the "Issues" section of the repository for my current hit list.

If you have suggestions for other features I am open to hearing them. Use the "Issues" section of the repository to start a conversation.

Metadata

Release files for django-lookup-tables 0.15.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-lookup-tables 0.15.3
File Size Uploaded Provenance
django-lookup-tables-0.15.3.tar.gz 231.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-lookup-tables 0.15.3
File Interpreter ABI Platform
django_lookup_tables-0.15.3-py3-none-any.whl Python 3 none any Details

Total release size: 249.8 kB

Release files / django-lookup-tables-0.15.3.tar.gz

Download URL django-lookup-tables-0.15.3.tar.gz
Size 231.7 kB
Tags Source
SHA-256 checksum
How to use checksums
022680a9dc3592ceea54f4939700587c6ae92418c011ea3b797f445ef4ae467c
BLAKE2b-256 checksum
How to use checksums
e91a26b5fbfdba73ec443471a9fd534c50b2a7c3aa804c2407d89e3578001b1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

Release files / django_lookup_tables-0.15.3-py3-none-any.whl

Download URL django_lookup_tables-0.15.3-py3-none-any.whl
Size 18.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
454cbf19eddf4e07ae462ed1a1cfba97e8bc02898b8aad1d6955efb39958c369
BLAKE2b-256 checksum
How to use checksums
156cc6fc9179e84f9c962151a189ff31d51961b6609ccf8d061ba89bf45367ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

Security for django-lookup-tables 0.15.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page