Skip to main content

django-uidfield is a library which includes class UIDField for models.

Project description

PyPI latest TravisCI

About

Pretty UID fields for your Django models, with customizable prefixes and controlled length. Tested vs. Python 3.5, 3.6, 3.7, 3.8, 3.9, 3.10 and Django 2.2, 3.1, 3.2, 4.0, 4.1.

Usage

See examples below. You can optionally inherit your models from UIDModel, which gracefully handles IntegrityError on saving UIDs, making up to 3 attempts with random UIDs. Integrity errors should be pretty rare if you use large enough max_length on your fields, but you may still want to use it for extra safety:

from django_uidfield.fields import UIDField

class YourModel(models.Model):
    uid_field = UIDField(prefix='tmp_', max_length=20)

# the value will be like 'tmp_Akw81LmtPqS93dKb'

or:

from django_uidfield.models import UIDModel
from django_uidfield.fields import UIDField


class YourModel(UIDModel):
    uid_field = UIDField(prefix='tmp_', max_length=20)

Adding a UIDField to an existing model

You can populate the field with a data-migration:

def populate_uid(apps, schema_editor):
    User = apps.get_model("users", "User")

    for user in User.objects.all():
        user._meta.get_field("uid").populate(user, force_renew=True)
        user.save()


class Migration(migrations.Migration):
    operations = [migrations.RunPython(code=populate_uid)]

Note that the 3-attempt deduplication mechanism will not work, and you can get an error if you have a lot of objects and a small max_length.

Changelog

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

django-uidfield-0.3.4.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file django-uidfield-0.3.4.tar.gz.

File metadata

  • Download URL: django-uidfield-0.3.4.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for django-uidfield-0.3.4.tar.gz
Algorithm Hash digest
SHA256 dbb127d552569ace7292438cb872afe006654f009e1004ad2697b54b7ae35f23
MD5 7ca9bc0b1a10821726c90d1fd279a23a
BLAKE2b-256 14f11bf2876c2bd5cdeca5736daa3b1f13e5bb76838d2237669441b2f6975ded

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