3👍
Can this be done? Yes. But don’t do it.
Make it an integer.
Incrementing is then trivial – automatic if you make this the primary key. For searching, you convert the string to an integer and search the integer – that way you don’t have to worry how many leading zeros were actually included as they will all be ignored. Otherwise you will have a problem if you use 6 digits and the user can’t remember and puts in 6 0’s + the number and then doesn’t get a match.
5👍
A times ago I made something similar
You have to convert your string to int and then you must to get its length and then you have to calculate the number of zeros that you need
code = "00000"
code = str(int(code) + 1 )
code_length = len(code)
if code_length < 5: # number five is the max length of your code
code = "0" * (5 - code_length) + code
print(code)
- [Django]-How to check if, elif, else conditions at the same time in django template
- [Django]-Django AMQP error
- [Django]-Django rest framework ModelSerializer runs too slowly
0👍
number = int('00000150')
('0'*7 + str(number+1))[-8:]
This takes any number, adds 1, concatenates/joins it to a string of several (at least 7 in your case) zeros, and then slices to return the last 8 characters.
IMHO simpler and more elegant than measuring length and working out how many zeros to add.
- [Django]-Django Nonrel – Working around multi-table inheritance with noSQL?
- [Django]-Factory boy error : ValueError: save() prohibited to prevent data loss due to unsaved related object
- [Django]-How to save ManyToMany field in second database
0👍
For those who want to just increase the last number in a string.
Import re
a1 = 'name 1'
num0_m = re.search(r'\d+', str(a1))
if num0_m:
rx = r'(?<!\d){}(?!\d)'.format(num0_m.group())
print(re.sub(rx, lambda x: str(int(x.group()) + 1), a1))