mardi 3 août 2021

Best way to store multiple database details in Django

I would like to store the details of multiple databases that the django rest api will eventually connect to and retrieve the data.

Initially I created a model that looked like this:

class DB_Connector(models.Model):
    server  = models.CharField(max_length=250)
    database = models.CharField(max_length=100)
    db_username = models.CharField(max_length=100)
    db_password = models.CharField(max_length=256)
    table = models.CharField(max_length=250 , null=True)
    port = models.IntegerField(default=5432)
    def __str__(self) -> str:
        return self.server

The problem is the password field, I cannot hash it since otherwise I can't get the raw db_password to connect to the database. Should I use an encryption and decryption algortihm to secure the db_password field? The model of the Project looks like this (this model describes where to get the data for a specific project) :

class Project(models.Model):
    name = models.CharField(max_length=150)
    default_db_connector = models.OneToOneField(DB_Connector, on_delete=models.CASCADE, unique=False, related_name='site_connector_default', null=True )
    alternative_db_connectors = models.ManyToManyField(DB_Connector , related_name='site_connector_alternatives')

Aucun commentaire:

Enregistrer un commentaire