0

I have a DatabaseConnection schema with these attributes: db_type, db_connection_details.

Now, for my flask app, I am supporting two database types: mysql and postgresql and I have two different schemas for both pertaining to the db_connection_details schema which have different fields.

Now, in the DatabaseConnection schema, is it possible to specify which schema to pick depending on the db_type. Thanks!

Edit:

An example input is like:

"training_data": {
      "source": "mysql",
      "connection_details": {
        "a": "",
        "b": ""
      }
}

DatabaseConnection schema:

class DatabaseConnection(Schema):
    source = fields.String(required=True)
    connection_details = fields.Nested(ConnectionSchema, required=True)

Now the mysql schema has a, b fields (as shown in the example), while the postgres schema has additional fields c, d. If the source is a mysql database, it should pick up the MySqlConnectionSchema, while if the source is a postgresql database, it should pick up PostgresConnectionSchema (in place of the ConnectionSchema field in DatabaseConnection schema). Is this possible in marshmallow?

harshit-sh
  • 358
  • 1
  • 3
  • 17

1 Answers1

0

Have you tried subclassing the schema and dynamically selecting? Something like this. I haven't tried or tested it so just an idea.

class MySql(object):
     a = 1
     b = 2

class Postgres(object):
     a = 1
     b = 2
     c = 3
     d = 4

if _conn = 'mySql':
    SubClass = MySql
else:
    SubClass = Postgres

class DatabaseConnection(Schema, SubClass):
    ...
Attack68
  • 4,437
  • 1
  • 20
  • 40