0

I have to create a unique order_reference field.

I read that I should use UUID for that. Can you recommend that? And is that here the right approach to it? (I found this snippet in another project)

uuid.UUID(bytes=base64.urlsafe_b64decode('%s==‘ % base64_uuid))

I don't understand what urlsafe_b64decode is doing.

Joey Coder
  • 3,199
  • 8
  • 28
  • 60

2 Answers2

0

You can use the uuid module for that:

import uuid as uuid_module

class MyModel(Model):
    uuid = UUIDField(unique=True, default=uuid_module.uuid4, editable=False)
Risadinha
  • 16,058
  • 2
  • 88
  • 91
0

Are you using that in a model or form?

You can use UUIDField which is recommended usage. The database will not automagically create your uuid so you will have to pass a callable as

import uuid
from django.db import models

class MyUUIDModel(models.Model):    
    order_reference = models.UUIDField(default=uuid.uuid4, editable=False)

Look at the documentation for further reference.

santi_
  • 191
  • 1
  • 8