1

I have the same question/problem than this post -> peewee - modify db model meta (e.g. schema) dynamically . I want to change the schema field in my Meta class dynamically. This is my code:

class GPSPosition(Model):

    def __init__(self, esquema, vehiculo, fechaFrom):
        self.esquema = esquema + '_org'
        self.vehiculo = vehiculo
        self.fechaFrom = fechaFrom

    orgid = BigIntegerField()
    id = BigIntegerField()
    vehicleid = BigIntegerField()
    driverid = BigIntegerField()
    originaldriverid = BigIntegerField(null=False)
    blockseq = IntegerField(null=False)
    time = DateTimeField(null=False)
    latitude = FloatField(null=False)
    longitude = FloatField(null=False)
    altitude = SmallIntegerField(null=False)
    heading  = SmallIntegerField(null=False)
    satellites  = SmallIntegerField(null=False)
    hdop  = FloatField(null=False)#float
    ageofreading = IntegerField(null=False)
    distancesincereading = IntegerField(null=False)
    velocity = FloatField(null=False)
    isavl = BooleanField(null=False)
    coordvalid = BooleanField(null=False)
    speedkilometresperhour = DecimalField(null=False)
    speedlimit = DecimalField(null=False) 
    vdop = SmallIntegerField(null=False)
    pdop = SmallIntegerField(null=False)
    odometerkilometres = DecimalField(null=False)
    formattedaddress = CharField(null=False)
    source = CharField(null=False) 

    class Meta:
        database = db
        schema = esquema
        db_table = 'test_gpspositions'
        primary_key = CompositeKey("orgid", "id")

Can someone please show me the light about this? Thanks!

1 Answer 1

3

Well I'll answer my own question since I found the answer time ago and it's very simple, just add this 1-2 lines at the point you want to change the schema name:

schemaname = 'your_schema_name'
setattr(YourPeeweeModel._meta, "schema", schemaname)

Works fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.