0

How does one generate integer of 8 length unique integers for CharField() I want to use this to generate barcode id

Eg maybe a function? I have seen the uuid.uuid4 but it's too much for what I needed. or maybe there is a way to cut it down to 8 digits only?

Martins
  • 1,130
  • 10
  • 26
  • So you want to generate 8 digits random integers? – Dani Mesejo Oct 25 '18 at 12:43
  • Did you look at the [`random` module](https://docs.python.org/3/library/random.html) yet? Are you talking about *digits* or an integer value between 10000000 and 99999999 (so are leading zeros allowed)? – Martijn Pieters Oct 25 '18 at 12:44
  • 1
    I duped you to the canonical 'generate a random string' question because for a `CharField` you want a string result, not an integer, and a barcode allows leading zeros. You'd use `''.join([random.choice(string.digits) for _ in range(8)])` in that case. – Martijn Pieters Oct 25 '18 at 12:47
  • @MartijnPieters You can also use `random.choices(string.digits, k=8)` instead. – blhsing Oct 25 '18 at 12:49
  • @blhsing: in Python 3.6 and up, yes. I'm not always making that assumption, the `random.choice(string.digits)` option in a loop works on all Python versions, including Python 2.x. – Martijn Pieters Oct 25 '18 at 12:51
  • @MartijnPieters Good point. Agreed. – blhsing Oct 25 '18 at 12:52
  • @MartijnPieters it always gives unique? – Rohit-Pandey Oct 25 '18 at 12:52
  • @blhsing: besides, `random.choices(seq, k=num)` is literally a `[choice(seq) for _ in range(num)]` loop under the hood. – Martijn Pieters Oct 25 '18 at 12:54
  • @Rohit-Pandey: define 'unique'. It produces 8 random digits. There is no definition of 'uniqueness' in the question. There is a 1 in 100 million chance that you generate a sequence you generated before, because there are only 100 million different combinations you can make with 8 digits. – Martijn Pieters Oct 25 '18 at 12:55
  • `How does one generate integer of 8 length unique integers`: that is the question statement that why i ask. – Rohit-Pandey Oct 25 '18 at 12:58
  • @Rohit-Pandey: and 8 unique digits sounds like a misunderstanding here. You can use `random.sample(string.digits, 8)` if you must have 8 digits that are all different, but the number of such combinations is much smaller than 100 million (about 1.8 million). – Martijn Pieters Oct 25 '18 at 12:59
  • Understand @MartijnPieters – Rohit-Pandey Oct 25 '18 at 13:01
  • @MartijnPieters thanks for pointing that out. let me look into the link and see if it matches what I was looking for. – Martins Oct 25 '18 at 16:00
  • @MartijnPieters what I have is a Charfield with a uniqe constraint. what I did before was something like this `barcode = models.CharField(max_length = 8, unique=True)` I overide the save method to save the the id of the object in place were the barcode number is not provided. now I think the best thing to do is to follow what you said i should follow because my previous solution had me saving twice in other to pupluate the barcode field twice. – Martins Oct 25 '18 at 16:05
  • @Martins: then use a loop, generate a random string of digits (`''.join(random.choices(string.digits, k=8))`) and try to insert. If there was an exception, loop and generate a new string of digits. Break out of the loop when the insert succeeds. – Martijn Pieters Oct 25 '18 at 16:06

0 Answers0