I've got a model with a UUID primary key:
id = models.UUIDField(
primary_key = True,
default = uuid.uuid4,
editable = False,
null = False,
blank = False,
help_text = 'The unique identifier of the Node.'
)
The issue I'm trying to solve is that Django always seems to try an Update before an Insert.
The Django Docs seem to be explicit on what the behavior should be: If pk is None then it will try an INSERT first, otherwise an UPDATE.
When I create a ModelInstance, it autopopulates id with a UUID, so it's not none. Is there a way I can make it not do this?
Regardless, even if I set id and pk to None on create, it still does an UPDATE first:
my_totally_new_node = Node(data = data, id = None, pk = None)
my_totally_new_node.id
> None
my_totally_new_node.pk
> None
reset_queries()
my_totally_new_node.save()
connection.queries
> [{'sql': 'UPDATE "connection_p...::uuid", 'time': '0.002'}, {'sql': 'INSERT INTO "connect...::bytea)", 'time': '0.001'}]
To summarize:
- Is there a way I can cause default behavior for the ID field to be not to populate on create, but to still populate on Save?
- Any ideas why Django isn't following the documented behavior when
pkisNoneand is still trying anINSERTfirst?
I'm aware of the save(force_insert=True) workaround but I'm looking for a better way, partly because create doesn't support this workaround.