Currently the top answer for How does a Django UUIDField generate a UUID in Postgresql? says
When you use
UUIDField
as a primary key in Django, it doesn't generate a UUID one for you, you generate it yourself before you save the object
But I think it should be possible to hack a solution that allows for DB-based UUID generation.
Once you have installed uuid-ossp
extension in postgres
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
I think it should work more or less like an AutoField
.
If a raw sql has been used for the table creation
CREATE TABLE example(
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
);
Then raw insert will return a new random UUID as an id field.
How would one go about creating a custom Field
so this raw solution works with Django ORM?