I would like a model to generate automatically a random alphanumeric string as its primary key when I create a new instance of it.
example:
from django.d
One of the simplest way to generate unique strings in python is to use uuid
module. If you want to get alphanumeric output, you can simply use base64 encoding as well:
import uuid
import base64
uuid = base64.b64encode(uuid.uuid4().bytes).replace('=', '')
# sample value: 1Ctu77qhTaSSh5soJBJifg
You can then put this code in the model's save
method or define a custom model field using it.