What I need to do is run a function and append a prefix to the beginning of the results returned from that function. This needs to be done each time a new instance is created of my model.
What I have tried....
The following won't work because you cannot add a string to a function and would set the ID as s_<function name etc>
and not the results of the function.
APP_PREFIX = "_s"
id = models.CharField(primary_key=True, max_length=50, unique=True,
default="{}{}".format(APP_PREFIX, make_id))
Nor will passing the prefix to the function because Django will generate the same key each time calling the function this way, no idea why tho:
id = models.CharField(primary_key=True, max_length=50, unique=True,
default=make_id(APP_PREFIX))
This won't work either:
id = models.CharField(primary_key=True, max_length=50, unique=True,
default=make_id + APP_PREFIX)
Or this:
id = models.CharField(primary_key=True, max_length=50, unique=True,
default=make_id() + APP_PREFIX)
How can this be achieved?
I could overwrite the save()
method and achieve this but there must be a way to do this with the default parameter on the field!