Universally Unique Lexicographically Sortable Identifier (ULID) support in Django
Project description
Universally Unique Lexicographically Sortable Identifier (ULID) support in Django.
Status
This project is actively maintained.
Installation
To install django-ulid from pip:
$ pip install django-ulid
To install ulid from source:
$ git clone git@github.com:ahawker/django-ulid.git
$ cd django-ulid && python setup.py install
Usage
Adding a ULID field to your Django models is straightforward. It can be a normal field or a primary key.
from django.db import models
from django_ulid.models import default, ULIDField
class Person(models.Model):
id = ULIDField(default=default, primary_key=True, editable=False)
Passing in default to the ULIDField will automatically create a default value using the ulid.new function. If you do not want a default value, None by default, feel free to omit it.
from django.db import models
from django_ulid.models import ULIDField
class Person(models.Model):
optional_id = ULIDField()
Adding a ULID field to your Django REST Framework serializers is also straightforward.
Simply importing the django_ulid.serializers module will automatically register the ULIDField serializer by overriding the serializer_field_mapping on the default ModelSerializer.
from django_ulid import serializers
If you are using a ULID as a primary key on a model, you need to create a custom PrimaryKeyRelatedField to automatically serialize the instance through the foreign key.
import functools
from django_ulid.serializers import ULIDField
from rest_framework import serializers
PersonPrimaryKeyRelatedField = functools.partial(serializers.PrimaryKeyRelatedField,
allow_null=True,
allow_empty=True,
pk_field=ULIDField(),
queryset=Person.objects.all())
class OrganizationSerializer(serializers.ModelSerializer):
owner = PersonPrimaryKeyRelatedField()
Contributing
If you would like to contribute, simply fork the repository, push your changes and send a pull request. Pull requests will be brought into the master branch via a rebase and fast-forward merge with the goal of having a linear branch history with no merge commits.
License
Dependencies
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_ulid-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bbede606c145c43ccde9a95f62a706041c3ed214664d7933778acbbeb584663 |
|
MD5 | a823d112d4acc9b69028bd7fce7bcab5 |
|
BLAKE2b-256 | 21efedbde3077466d418be330201476cf085b3d672195c333b8e76e7b1814404 |