0

I'm using Django Admin capabilities to add to a model a randomly generated password. However, doing refresh to the admin's form page or after clicking "Save and add another" this random password doesn't change.

How do i force Django to re-generate a fresh password?

def generate_rand():
    random_bytes = urandom(64)
    return b64encode(random_bytes).decode('utf-8')

class SubClient(models.Model):
    client = models.ForeignKey(Client, models.SET_NULL, blank=True, null=True,
                           verbose_name=_('Organization name'),)
    name = models.CharField(max_length=150,
                        verbose_name=_('End-Client name'),)

    db_password = models.CharField(max_length=255, default=generate_rand())
Yarh
  • 895
  • 7
  • 15

1 Answers1

1

Pass a callable object, not the result of the function:

db_password = models.CharField(max_length=255, default=generate_rand)

Django documentation for default

Community
  • 1
  • 1
Jens Astrup
  • 2,415
  • 2
  • 14
  • 20