How to Generate a Random and Unique Order ID for a Purchase with Python in Django


In this article, we show how to generate a random and unique order ID for a purchase with Python in Django.

When a customer on a website purchases something, most ecommerce sites create a random an order ID. This order ID is usually unique, so that if a customer calls support for a partcular purchase, s/he can reference the order ID number, and the support staff can just look up the purchase through that number.

This is a smart practice.

The order ID should be unique, so that it only references a single order.

Of course, you can go off of the id attribute of the Order database table that you create for each order, but this isn't best practice. Because if you do it this way, a customer can know exactly how many orders there have been on your ecommerce. Imagine a customer getting an order ID of 4. S/he could then make another purchase and get an order ID of 5. At that point, s/he could just figure out that the orders on the site are sequential and would know they're just the 4th or 5th order on the site. At that point, they would know that the site barely has any customers, which you don't want.

Therefore, it's best to generate a random order ID.

We can write Python code within the Django framework to create this order ID. File

Inside of the file, we have our Order model.

We also must insert the value of the order_id into the order_id field using a pre_save signal in Django.

So the first thing is we have to import the Cart model we are created in the carts app. This carts model contains all the items that the user has purchased. We make this Cart model a ForeignKey to this Order model.

We then import a function, unique_order_id_generator, which we will create in the file within this orders app.

We then have to import pre_save from django.db.models.signals

We then create a tuple. This will contain the various order statuses, such as 'Not Yet Shipped', 'Shipped', 'Cancelled', 'Refunded'. We put this in the status form field of the Order model.

We next create a model called Order.

So we have a number of fields.

For the purpose of this article, the only field we are interested in is the order_id form field.

We create a function, pre_save_create_order_id, to presave the value of the field, order_id. If instance.order_id is None, we set order_id to, unique_order_id_generator(instance)

What this does is it executes the function in the file and returns a random and unique order_id, which then gets saved to the Order model through a pre_save signal.

So next we go to the random order ID generator function in the file.

Random, Unique Order ID Generator

So the next thing is we must now create the random order ID generator. We do this in the file of our app, though we can also put it in the app of the project directory.

Below is the code in the file.

We create first a function, random_string_generator. This function will generator a random string of 10 characters.

We then create a function, unique_order_id_generator

This function creates a random string with the random_string_generator() function and places it within the order_new_id variable.

We then check to see if this order ID exists already within the database. If it exists, we start the function back from the start with the line, return unique_order_id_generator(instance). If it does not exists, we return the value, order_new_id

The value, order_new_id, is what gets inserted into the order_id form field of the Order database

Now each time an instance of the Order model is created, a random and unique order ID will be created.

And this is how to generate a random and unique order ID with Python in Django.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...