9👍
You should pass the callable as default, not the result of a call, like:
class HasUUID(models.Model):
name = models.CharField(max_length=10)
batchid = models.UUIDField(default=uuid.uuid4, unique=True)
Notice that there are no paratheses here to make the call, we thus pass a reference to the uuid4
function itself.
The default=
value is not a specific UUID (that is determined when you start the server), it should be a value that is determined when you create a new object (without specifying the batchid
yourself).
By passing a callable, Django will understand that the default is the result of a call to the callable, and it will encode that in the migration. By calling the function, you retrieve the result of the call, and each time you run makemigrations
, Django will think that you changed your mind on what should be the default value (it will first think you want to use '3b96231c-5848-430b-aa90-b6e41b11fd0a'
as default, and later that you want to use '335c3651-b04e-4ed8-a91d-f2da3f53dd8f'
). By passing a callable, the value you pass as default remains the same.