Search code examples
djangouuid

uuid as keyword argument <pk> in django urls.py


I am using a uuid4 as id in some django models. Thats why I cannot use <int:pk> in the urls.py because it wont match. Is there another prefix I can use to match these uuids like a2182835-4518-cb95-8eaf-0d5a34105cb2 ?


Solution

  • You can use the uuid path converter instead of int. From the docs

    uuid - Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example, 075194d3-6885-417e-a8a8-6c931e272f00. Returns a UUID instance.

    So for your purposes changing <int:pk> to <uuid:pk> should do the trick