'How to execute a method within a class without calling it, with self parameter

I am trying to make a randomized urls and save them in a database but this code keep return this error

TypeError: randomizer() missing 1 required positional argument: 'self'

I Tried to call The randomizer as

randomizer() 

instead of

randomizer

but this does not work because it will generate random characters for once. It is the same when with qr_generator function it requires the self argument.

class Table(models.Model):

     def randomizer(self):
        qr  = str(self.restaurant.id).join((random.choice(string.ascii_letters) for x in range(5))) 
        return qr

    def qr_generator(self):
        url = DOMAIN +"/table/" + self.qr
        qr_img = qrcode.make(url)
        qr_img.save(f"qrs/{self.restaurant.id}/{self.qr}.png")
        return f"qrs/{self.restaurant.id}/{self.qr}"
    
    number = models.IntegerField()
    restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
    qr = models.CharField(max_length=50, default=randomizer, unique=True)
    qr_img = models.CharField(default=qr_generator, max_length=100)


Solution 1:[1]

use static method

import random


class Test():
    name = "joe"

    @staticmethod
    def name_with_random_number():
        print(Test.name+str(random.randint(0, 1000)))

    name_number = name_with_random_number


print(Test.name_number())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Udesh Ranjan