I need a custom alphanumeric sequence for a custom_id on django model for each Product.
def save(self, *args, **kwargs):
if self.custom_id is None:
self.custom_id = f"{custom_seq(self.id)}"
return super(Product, self).save(*args, **kwargs)
I want custom Alphanumeric sequence to be generated as:
eg. letters=4, digits=3 then,
'AAAA001', 'AAAA002', 'AAAA003'... 'ZZZZ999'
if letters =3 and digits =4
'AAA0001', 'AAA0002'... 'ZZZ9999'
Here is my try:
def custom_seq(pk, letter=4, digits=3):
init = ''
alpha = string.ascii_uppercase
for i in alpha:
for j in alpha:
for k in alpha:
for l in alpha:
yield(i+j+k+l)
This will genrate only 'AAAA' to 'ZZZZ', but not sure how to add digits in front of it and make this dynamic.
itertoolshas a function,product, which might be of help.product, Dynamic means- in my code you might have observed I have written 4 for looks when letters are 4. Those 4 loops should dynamic, so that if I change thelettersto some othernumbersay 3 or 5 for loops should change for that number of times.self.idto do when you passed it tocustom_seq? Seems like you want a custom generator to yieldAAA001... , but how do you want to control where does the sequence start from? Like do you want to control whether first sequence isAAA001orAAA999?itselfinstead of digits but the problem was it could have any number of digits such as 2 digits 3 digits etc. which would vary my sequence length