peewee - change schema dynamically

半世苍凉 提交于 2021-01-29 11:14:13

问题


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:


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.



来源:https://stackoverflow.com/questions/64246772/peewee-change-schema-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!