Search code examples
pythoninitializationinfinite-loopdjango-errors

Django Runtime Error: calling method in __init__ causes maximum recursion depth exceeded


I want to call password_generator method when Register class is instantiated and assign the value return from from the method to password field.

When the register object is instantiated, why do I get

RuntimeError at /register/

maximum recursion depth exceeded while calling a Python object

This is the code

models.py

class Register(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    email = models.EmailField(max_length=120, blank=False, null=False)
    password = models.TextField(blank=False, null=False)
    firstname= models.CharField(max_length=120, blank=False, null=False)
    lastname = models.CharField(max_length=120, blank=False, null=False)
    phonenumber = models.CharField(max_length=120, blank=True, null=True)

    def __init__(self, *args, **kwargs):
        self.password = Register().password_generator()

    #Generate random login password for candidate
    def password_generator(self, size=30, chars=string.ascii_letters + string.digits + string.punctuation):
        return ''.join(random.choice(chars) for i in range(size))

Logs from the terminal

console

  File "D:\Project\Online-Examination-System\register\models.py", line 31, in __init__
    self.password = Register().password_generator()
  File "D:\Project\Online-Examination-System\register\models.py", line 31, in __init__
    self.password = Register().password_generator()
  File "D:\Project\Online-Examination-System\register\models.py", line 31, in __init__
    self.password = Register().password_generator()
  File "D:\Project\Online-Examination-System\register\models.py", line 31, in __init__
    self.password = Register().password_generator()
  File "D:\Project\Online-Examination-System\register\models.py", line 31, in __init__
    self.password = Register().password_generator()
  File "D:\Project\Online-Examination-System\register\models.py", line 31, in __init__
    self.password = Register().password_generator()
  File "D:\Project\Online-Examination-System\register\models.py", line 31, in __init__
    self.password = Register().password_generator()
  File "D:\Project\Online-Examination-System\register\models.py", line 31, in __init__
    self.password = Register().password_generator()
  File "D:\Project\Online-Examination-System\register\models.py", line 31, in __init__
    self.password = Register().password_generator()

How to fix this?


Solution

  • After many attempts, I came to realize that the function that I'm trying to call is in infinite loop. To access password_generator() method, I have to do Register().password_generator(). So that's also means __init__ method is called- because of Register(). In the __init()__, I again call Register(). This is why it becomes an infinite loop. To solve this, we have to use:

        def __init__(self, *args, **kwargs):
        self.password = self.password_generator()