This is an old one but the way to go would have been to encrypt the password before you stored it. Using a key stored securely (i.e. not in the DB (or at least not in the same one) and not in Github) and to load that into the environment where the project was deployed. This way anyone gaining access to the DB directly wouldn't be able to read the password.
possible source of more info
The Cryptography library is a good one to use, and is referenced in the article above.
For the case described in this question, I would have place an encryption key in the environment and loaded it to settings.py
on deployment it would look something like this:
generating a key
from cryptography.fernet import Fernet
key = Fernet.generate_key()
You could run the above, if the library is installed in to your Python environment, in a python console.
Depending on how you deploy the app you would need to provide access to the key to it. Below is one way to get it into your environment:
export ENCRYPTION_KEY="<key>"
Once you have it in your environment in settings add this line
ENCRYPTION_KEY=os.get("ENCRYPTION_KEY")
How to use the key? Depending on your setup you could write additional property method on your model to get and set the value of the field, something like
class mymodel(models.Model):
....
@property
def password(self):
f = Fernet(settings.ENCRYPTION_KEY)
password = f.decrypt(self.<myfield>)
return password
@password.setter
def password(self, password):
f = Fernet(settings.ENCRYPTION_KEY)
token = f.encrypt(<password>)
self.<myfield> = token
self.save()
Other options could be to subclass another django model field and add the functionality there. If you're only doing this in one place then the above should be fine.
How you store the Encryption Key is up to you the environment is just one option. Key here is to keep it somewhere safe and importantly don't lose it... or good luck ;)