Django Slugs and UUIDs
Why We have to Use UUID or SlugFlied:
The django default id or primary_key( pk ) is currently auto-incrementing id. Among other concerns, it tells a potential hacker exactly how many records you have in your database; it tells them exactly what the id is which can be used in a potential attack; and there can be synchronization issues if you have multiple front-ends.
There are two alternative approaches. UUID and SlugFiled
UUIDField:
A field for storing universally unique identifiers. Uses Python’s UUID class. When used on PostgreSQL and MariaDB 10.7+, this stores in a uuid datatype, otherwise in a char(32).
Example Code:
SlugField:
Slug is a newspaper term. A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs. Like a CharField, you can specify max_length. If max_length is not specified, Django will use a default length of 50.
Example Code:
For example, in our example of “Django for Professionals” its slug could be django-for-professionals. There’s even a SlugField model field that can be used and either added when creating the title field by hand or auto-populated upon save.
The main challenge with slugs is handling duplicates though this can be solved by adding random strings or numbers to a given slug field. The synchronization issue remains though. A better approach is to use a UUID (Universally Unique IDentifier) which Django now supports via a dedicated UUIDField
Comments
Post a Comment