django-unique-random - Django model that generates unique random codes upon saving
This Django app consists of a models.py file whose UniqueRandom model generates such codes. You can modify UniqueRandom to store other fields/relationships, but it's probably easier just to copy its save() method to one of your own models and sync/migrate your database. The save() method works by making random codes of a given length. When a code is found that hasn't already been used, it's assigned to the current object. Obviously this method could be problematic for short codes and/or small alphabets; for a discussion, see this Stack Overflow post (tl;dr: working with a base-10 alphabet, you're more than fine with 12-digit codes).