How to Generate a Random and Unique Video ID for an Upload with Python in Django



Python


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

To uniquely identify a video publicly, youtube takes the method of creating a random, unique 11-digit video id for each video.

Since youtube generates an 11-digit video id and each character can be taken from 64 possible characters, this means that with an 11-digit random character, there are which is equal to 73,786,976,294,838,206,464 possible combinations. That's a lot of unique identifiers.

Why does youtube take this approach for identifying videos?

The main reason youtube most likely uses this is because public sequential indexing is a bad idea. If a user saw their video was number 1000, s/he might inquire to what is video 1001. And what was video 1002. People would know what video number was uploaded to youtube (I uploaded the 10 millionth video to youtube).

To bypass this, a random, unique video id is assigned to each video uploaded to the site.

We can write Python code within the Django framework to create this random, unique video ID.


models.py File

Inside of the models.py file, we have our Video model.

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



So the first thing we have to do is import a function, unique_video_id_generator, which we will create in the utils.py file within this videos app.

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

We next create a model called Video.

So we have a number of fields.

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

We create a function, pre_save_create_video_id, to presave the value of the field, videoid. If instance.video_id is None, we set videoid to, unique_video_id_generator(instance)

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

So next we go to the random video ID generator function in the utils.py file.


Random, Unique Video ID Generator

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

Below is the code in the utils.py file.



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

We then create a function, unique_video_id_generator

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

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

The value, video_new_id, is what gets inserted into the videoid form field of the Video database.

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

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


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...