How do I create Stripe-like DB ID's for Django models?

Core problem I'm trying to solve: I want to have ID's on my database models that are similar to Stripe's (i.e. of the form aaa_ABCD1234 where the ABCD1234 part is a ULID and the aaa_ part is effectively the table name (or a shortened version of it)). This feature has saved me a ton of time in debugging Stripe integrations and I'd love for users of my systems to be able to have those same benefits. However, I know taking the nieve approach and just a string as a primary key on a table is terrible for performance (mostly indexes and consequently joins as I understand it) so I want to take advantage of the built in UUID datatype in the DB to store only the ABCD1234 part in the DB and not store the aaa_ part since that will be identical for all rows in that table.

Where I stand today

I'm currently thinking of doing this as follows:

  • Have a "private" field on the DB model (call it _db_id?) that is the UUID and technically the primary key on the model in the DB.
  • Have a GeneratedField that takes the above private field and prepends the table prefix I want onto it.

What I'm struggling with in this approach is how to get Django internally/with all it's automagicalness to use the generated field as the "public" primary key but use the _db_id field when talking to the DB/making joins/etc.

Is there a solution here I'm missing? Is there a better way to do this?

Back to Top