5

I'm working on a Python(3.6) & Django(1.10) project in which I need to save some user credentials of the third party services like username, password, and email, I'm implementing only rest API, so there's no form.py at all. So, How can I make hash fields inside models.py file?

Here's my current models.py:

class DeploymentOnUserModel(models.Model):
    deployment_name = models.CharField(max_length=256, )
    credentials = models.TextField(blank=False)
    project_name = models.CharField(max_length=150, blank=False)
    project_id = models.CharField(max_length=150, blank=True)
    cluster_name = models.CharField(max_length=256, blank=False)
    zone_region = models.CharField(max_length=150, blank=False)
    services = models.CharField(max_length=150, choices=services)
    configuration = models.TextField(blank=False)
    routing = models.TextField(blank=True)

    def save(self, **kwargs):
        if not self.id and self.services == 'Multiple' and not self.routing:
            raise ValidationError("You must have to provide routing for multiple services deployment.")
        super().save(**kwargs)

I want to add three new hash fields like username, password & email.

Help me, please!

Thanks in advance!

Abdul Rehman
  • 5,326
  • 9
  • 77
  • 150

1 Answers1

10

You can use standard CharField. To store hash value use make_password method before saving:

from django.contrib.auth.hashers import make_password

password = models.CharField(max_length=256)

def save(self, **kwargs):
    some_salt = 'some_salt' 
    password = make_password(self.password, some_salt)
    if not self.id and self.services == 'Multiple' and not self.routing:
        raise ValidationError("You must have to provide routing for multiple services deployment.")
    super().save(**kwargs)
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
  • 1
    What kind of salt can I generally use? – Abdul Rehman Aug 04 '18 at 05:11
  • and Do I need to call `make_password()` method for all fields need to hashed like `email` & `username` ? – Abdul Rehman Aug 04 '18 at 05:14
  • @AbdulRehman if you need this fields to be hashed then yes. As for salt you can actually not provided it to `make_password` method. In this case Django generate it automatically. – neverwalkaloner Aug 04 '18 at 05:18
  • Nice. But how do you verify? – theTypan Mar 27 '20 at 19:57
  • 1
    Just found how to verify https://docs.djangoproject.com/en/3.0/topics/auth/passwords/#module-django.contrib.auth.hashers – theTypan Mar 27 '20 at 19:59
  • **Note:** this example isn't actually setting the instance `password`. Would need to use `self.password = make_password(self.password, some_salt)` – Sam Morgan Apr 13 '22 at 16:55
  • you can avoid using a particular salt as the salt parameter is not mandatory for make_password(), this way django creates it's own salt which seems more secure to me. – Sangeeth Joseph Oct 23 '22 at 06:06
  • 1
    you can verify using check_password() function imported from django.contrib.auth.hashers module. You don't have to use any salt here if you haven't passed any salt at the make_password() django will use it's salt automatically. – Sangeeth Joseph Oct 23 '22 at 06:12