This is the first time that I ask a question here, so sorry if it's not perfect.
I'm working in django on the last version.
I would like to auto-generate a field (KEY (which will be used in dialogflow)) value when I submit my form with the concatenation of other fields as
KEY = 'SENT_' + search_categories + '_' + '001'
and I would like that the '001' will be auto-incremented too.
I can't show more code because I'm not sure that it's legal cause I'm working in a private firm but I think that I can show the key declaration in my django model.
key = models.CharField(max_length=30, blank=False, null=False, unique=True)
I hope that you'll help me!
Thank you very much!
You can generate your key in the save
function of your model :
class Model(models.model):
key = models.CharField(max_length=30, blank=False, null=False, unique=True)
# other attributes...
def save(self, *args, **kwargs):
# Only generate key on creating if it was not provided
if not self.id and not self.key:
# Get your counter and increment it
counter = 0
counter += 1
# Get search_categories (I don't know what it is)
search_categories = '?'
# Use f-string to concatenate the key and add zero pad on the counter
self.key = f'SENT_{search_categories}_{counter:03}'
super().save(*args, **kwargs)