peewee-动态更改架构 [英] peewee - change schema dynamically

查看:80
本文介绍了peewee-动态更改架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与此帖子相同的问题/问题-> peewee-修改数据库模型元数据(例如模式).我想动态更改Meta类中的架构字段.这是我的代码:

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-2行更改架构名称:

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)

工作正常.

这篇关于peewee-动态更改架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆