文档章节

python 从数据库表生成model

NathanJoy
 NathanJoy
发布于 2016/04/22 18:45
字数 1518
阅读 90
收藏 1

python 从数据库表生成model

 

找了很久才找到这个,我是新手...

现在已有建好的数据库,需要基于原有数据做数据分析的web应用,我选择python+Tornado ,由于不想写SQL语句,就想偷个懒

 

1、安装工具

1 ningjian@freegodly:~/code/py/django/logcloud$ sudo pip install sqlacodegen
2 Downloading/unpacking sqlacodegen
3   Downloading sqlacodegen-1.1.6-py2.py3-none-any.whl
4 Downloading/unpacking inflect>=0.2.0 (from sqlacodegen)
5   Downloading inflect-0.2.5-py2.py3-none-any.whl (58kB): 58kB downloaded
6 Requirement already satisfied (use --upgrade to upgrade): SQLAlchemy>=0.6.0 in /usr/local/lib/python2.7/dist-packages (from sqlacodegen)
7 Installing collected packages: sqlacodegen, inflect
8 Successfully installed sqlacodegen inflect
9 Cleaning up...

 

2、转换

ningjian@freegodly:~/code/py/django/logcloud$ sqlacodegen mssql+pymssql://name:password@ip/LogColudDB --outfile logcloude_model.py
ningjian@freegodly:~/code/py/django/logcloud$

 

3、查看,哈哈

ningjian@freegodly:~/code/py/django/logcloud$ cat logcloude_model.py
# coding: utf-8
from sqlalchemy import BigInteger, Column, DateTime, Float, ForeignKey, Integer, LargeBinary, T                   able, Unicode, text
from sqlalchemy.dialects.mssql.base import BIT
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()
metadata = Base.metadata


class AuthorityInfo(Base):
    __tablename__ = 'AuthorityInfo'

    ID = Column(BigInteger, primary_key=True)
    Description = Column(Unicode('max'), nullable=False)
    IsDelete = Column(BIT, nullable=False)
    AuthorityIndex = Column(Integer, nullable=False)
    AuthorityName = Column(Unicode('max'), nullable=False)


class CPKInfoHistory(Base):
    __tablename__ = 'CPKInfoHistory'

    PO = Column(Unicode(10), primary_key=True)
    ProcessName = Column(Unicode(50), nullable=False)
    Result = Column(Unicode('max'), nullable=False)
    LastLogID = Column(BigInteger, nullable=False)


class ComputerState(Base):
    __tablename__ = 'ComputerState'

    Name = Column(Unicode(50), primary_key=True)
    Ip = Column(Unicode(50), nullable=False)
    IsDelete = Column(BIT, nullable=False)
    LastDate = Column(DateTime, nullable=False)
    IsProceted = Column(BIT, nullable=False)


class DeviceInfo(Base):
    __tablename__ = 'DeviceInfo'

    ID = Column(BigInteger, primary_key=True)
    ClientName = Column(Unicode(50), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)
    LinesInfo_ID = Column(ForeignKey(u'LinesInfo.ID'), nullable=False, index=True)

    LinesInfo = relationship(u'LinesInfo')


class DisposeErrorCodeInfo(Base):
    __tablename__ = 'DisposeErrorCodeInfo'

    ID = Column(BigInteger, primary_key=True)
    NewOperation = Column(Unicode('max'), nullable=False)
    Status = Column(Unicode('max'), nullable=False)
    DisposeInfo_ID = Column(ForeignKey(u'DisposeInfo.ID'), nullable=False, index=True)
    ErrorCode_Info_ID = Column(ForeignKey(u'ErrorCode_Info.ID'), nullable=False, index=True)
    ErrorCode_OperationID = Column(BigInteger, nullable=False)
    WeightValue = Column(Integer, nullable=False)

    DisposeInfo = relationship(u'DisposeInfo')
    ErrorCode_Info = relationship(u'ErrorCodeInfo')


class DisposeErrorCodeInfoHistory(Base):
    __tablename__ = 'DisposeErrorCodeInfoHistory'

    ID = Column(BigInteger, primary_key=True)
    OperateTime = Column(DateTime, nullable=False)
    OldStatus = Column(Unicode('max'), nullable=False)
    NewStatus = Column(Unicode('max'), nullable=False)
    IsDelete = Column(BIT, nullable=False)
    WeightValue = Column(Integer, nullable=False)
    UserInfo_ID = Column(ForeignKey(u'UserInfo.ID'), nullable=False, index=True)
    DisposeErrorCodeInfo_ID = Column(ForeignKey(u'DisposeErrorCodeInfo.ID'), nullable=False, in                   dex=True)

    DisposeErrorCodeInfo = relationship(u'DisposeErrorCodeInfo')
    UserInfo = relationship(u'UserInfo')


class DisposeInfo(Base):
    __tablename__ = 'DisposeInfo'

    ID = Column(BigInteger, primary_key=True)
    Operation = Column(Unicode('max'), nullable=False)
    IsOK = Column(BIT, nullable=False)
    Description = Column(Unicode(1024))
    TouchTime = Column(DateTime, nullable=False)
    NoticeInfo_ID = Column(ForeignKey(u'NoticeInfo.ID'), nullable=False, index=True)
    UserTask_ID = Column(ForeignKey(u'UserTask.ID'), nullable=False, index=True)

    NoticeInfo = relationship(u'NoticeInfo')
    UserTask = relationship(u'UserTask')


class ErrorCodeInfo(Base):
    __tablename__ = 'ErrorCode_Info'

    ID = Column(BigInteger, primary_key=True)
    ErrorCode = Column(Unicode(10), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)


class ErrorCodeOperation(Base):
    __tablename__ = 'ErrorCode_Operation'

    ID = Column(BigInteger, primary_key=True)
    Operation = Column(Unicode('max'), nullable=False)
    WeightValue = Column(Integer, nullable=False)
    IsEnable = Column(BIT, nullable=False)
    ErrorCode_Info_ID = Column(ForeignKey(u'ErrorCode_Info.ID'), nullable=False, index=True)

    ErrorCode_Info = relationship(u'ErrorCodeInfo')


class FilesManage(Base):
    __tablename__ = 'FilesManage'

    ID = Column(BigInteger, primary_key=True)
    ClassName = Column(Unicode(50), nullable=False, index=True)
    Md5 = Column(Unicode(32), nullable=False)
    Data = Column(LargeBinary, nullable=False)
    Ver = Column(Integer, nullable=False)
    DateCreated = Column(Unicode(50), nullable=False)
    UpLoadUserName = Column(Unicode(50), nullable=False)
    Remarks = Column(Unicode('max'))
    Catagory = Column(Unicode(50), nullable=False)
    LocalFileName = Column(Unicode(50))


class LOGInfo(Base):
    __tablename__ = 'LOG_Info'

    ID = Column(BigInteger, primary_key=True)
    Po = Column(Unicode(10), nullable=False, index=True)
    ProcessName = Column(Unicode(10), nullable=False, index=True)
    User = Column(Unicode(10), nullable=False)
    ErrorCode = Column(Unicode(10))
    Log = Column(Unicode('max'), nullable=False)
    Barcode = Column(Unicode(50))
    Isn = Column(Unicode(50))
    Shift = Column(Unicode(10), nullable=False)
    TestResult = Column(Unicode(10), nullable=False, index=True)
    LastDate = Column(DateTime, nullable=False)
    ClientName = Column(Unicode(50), nullable=False, index=True)


class LinesInfo(Base):
    __tablename__ = 'LinesInfo'

    ID = Column(BigInteger, primary_key=True)
    Name = Column(Unicode(50), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)


class NoticeInfo(Base):
    __tablename__ = 'NoticeInfo'

    ID = Column(BigInteger, primary_key=True)
    Top1_ErrorCodeID = Column(BigInteger, nullable=False)
    Top2_ErrorCodeID = Column(BigInteger)
    Top3_ErrorCodeID = Column(BigInteger)
    TouchTime = Column(DateTime, nullable=False)
    IsDispose = Column(BIT, nullable=False)
    TaskLavel = Column(Integer, nullable=False)
    Responsibility_ID = Column(ForeignKey(u'Responsibility.ID'), nullable=False, index=True)
    UserTask_ID = Column(ForeignKey(u'UserTask.ID'), nullable=False, index=True)
    DeviceInfo_ID = Column(ForeignKey(u'DeviceInfo.ID'), nullable=False, index=True)
    StatisticsInfo_ID = Column(BigInteger, nullable=False)

    DeviceInfo = relationship(u'DeviceInfo')
    Responsibility = relationship(u'Responsibility')
    UserTask = relationship(u'UserTask')


class POInfo(Base):
    __tablename__ = 'POInfo'

    ID = Column(BigInteger, primary_key=True)
    Po = Column(Unicode(10), nullable=False)
    Plm = Column(Unicode(20))
    ProductName = Column(Unicode(50))
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)
    Customer = Column(Unicode(50))


class Responsibility(Base):
    __tablename__ = 'Responsibility'

    ID = Column(BigInteger, primary_key=True)
    ProcessName = Column(Unicode(10), nullable=False)
    BaseNumber = Column(Integer, nullable=False)
    ErrorRate = Column(Float(53), nullable=False)
    Description = Column(Unicode(1024))
    OverTime = Column(Integer, nullable=False)
    POInfo_ID = Column(ForeignKey(u'POInfo.ID'), nullable=False, index=True)
    UserInfo_ID = Column(ForeignKey(u'UserInfo.ID'), nullable=False, index=True)

    POInfo = relationship(u'POInfo')
    UserInfo = relationship(u'UserInfo')


class RoleAuthority(Base):
    __tablename__ = 'RoleAuthority'

    ID = Column(BigInteger, primary_key=True)
    IsDelete = Column(BIT, nullable=False)
    RoleInfo_ID = Column(ForeignKey(u'RoleInfo.ID'), nullable=False, index=True)
    AuthorityInfo_ID = Column(ForeignKey(u'AuthorityInfo.ID'), nullable=False, index=True)

    AuthorityInfo = relationship(u'AuthorityInfo')
    RoleInfo = relationship(u'RoleInfo')


class RoleInfo(Base):
    __tablename__ = 'RoleInfo'

    ID = Column(BigInteger, primary_key=True)
    RoleName = Column(Unicode(256), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)
    RoleLevel = Column(Integer, nullable=False)


class SettingInfo(Base):
    __tablename__ = 'SettingInfo'

    ID = Column(BigInteger, primary_key=True, nullable=False)
    Key = Column(Unicode(50), primary_key=True, nullable=False)
    Value = Column(Unicode('max'), nullable=False)
    Description = Column(Unicode(1024))


class StatisticsInfo(Base):
    __tablename__ = 'StatisticsInfo'

    ID = Column(BigInteger, primary_key=True)
    ProcessName = Column(Unicode(10), nullable=False)
    BeginTime = Column(DateTime, nullable=False)
    NowErrorRate = Column(Float(53), nullable=False)
    Times = Column(Integer, nullable=False)
    IsOutmoded = Column(BIT, nullable=False)
    POInfo_ID = Column(ForeignKey(u'POInfo.ID'), nullable=False, index=True)
    DeviceInfo_ID = Column(ForeignKey(u'DeviceInfo.ID'), nullable=False, index=True)

    DeviceInfo = relationship(u'DeviceInfo')
    POInfo = relationship(u'POInfo')


class UserInfo(Base):
    __tablename__ = 'UserInfo'

    ID = Column(BigInteger, primary_key=True)
    Name = Column(Unicode(10), nullable=False)
    JobNumber = Column(Unicode(10), nullable=False)
    Phone = Column(Unicode(20))
    Emil = Column(Unicode(30), nullable=False)
    Department = Column(Unicode(20))
    Duties = Column(Unicode(20))
    Description = Column(Unicode(1024))
    Group = Column(Unicode(20), nullable=False)
    IsDelete = Column(BIT, nullable=False)
    Password = Column(Unicode(32))
    CreateDateTime = Column(DateTime, nullable=False)


class UserProfile(Base):
    __tablename__ = 'UserProfile'

    UserId = Column(Integer, primary_key=True)
    UserName = Column(Unicode(56), nullable=False, unique=True)


class UserRoleInfo(Base):
    __tablename__ = 'UserRoleInfo'

    ID = Column(BigInteger, primary_key=True)
    IsDelete = Column(BIT, nullable=False)
    UserInfo_ID = Column(ForeignKey(u'UserInfo.ID'), nullable=False, index=True)
    RoleInfo_ID = Column(ForeignKey(u'RoleInfo.ID'), nullable=False, index=True)

    RoleInfo = relationship(u'RoleInfo')
    UserInfo = relationship(u'UserInfo')


class UserTask(Base):
    __tablename__ = 'UserTask'

    ID = Column(BigInteger, primary_key=True)
    TaskLavel = Column(Integer, nullable=False)
    Shift = Column(Unicode(10), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)
    UserInfo_ID = Column(ForeignKey(u'UserInfo.ID'), nullable=False, index=True)
    LinesInfo_ID = Column(ForeignKey(u'LinesInfo.ID'), nullable=False, index=True)

    LinesInfo = relationship(u'LinesInfo')
    UserInfo = relationship(u'UserInfo')


t_view_LinesState = Table(
    'view_LinesState', metadata,
    Column('ID', BigInteger, nullable=False),
    Column('ClientName', Unicode(50), nullable=False),
    Column('LinesInfo_ID', BigInteger, nullable=False),
    Column('count', Integer)
)


t_view_LogState = Table(
    'view_LogState', metadata,
    Column('Po', Unicode(10), nullable=False),
    Column('ProcessName', Unicode(10), nullable=False),
    Column('ErrorCode', Unicode(10)),
    Column('ClientName', Unicode(50), nullable=False),
    Column('ID', BigInteger),
    Column('count', Integer)
)


t_view_NoticeInfo = Table(
    'view_NoticeInfo', metadata,
    Column('ID', BigInteger, nullable=False),
    Column('TouchTime', DateTime, nullable=False),
    Column('Top1_ErrorCodeID', BigInteger, nullable=False),
    Column('Top2_ErrorCodeID', BigInteger),
    Column('Top3_ErrorCodeID', BigInteger),
    Column('IsDispose', BIT, nullable=False),
    Column('TaskLavel', Integer, nullable=False),
    Column('ClientName', Unicode(50)),
    Column('ProcessName', Unicode(10)),
    Column('BeginTime', DateTime),
    Column('NowErrorRate', Float(53)),
    Column('Times', Integer),
    Column('IsOutmoded', BIT),
    Column('Po', Unicode(10))
)


t_view_Top1_error = Table(
    'view_Top1_error', metadata,
    Column('Top1_ErrorCodeID', BigInteger, nullable=False),
    Column('count', Integer),
    Column('ID', BigInteger, nullable=False),
    Column('ErrorCode', Unicode(10), nullable=False),
    Column('Description', Unicode(1024))
)


class WebpagesMembership(Base):
    __tablename__ = 'webpages_Membership'

    UserId = Column(Integer, primary_key=True)
    CreateDate = Column(DateTime)
    ConfirmationToken = Column(Unicode(128))
    IsConfirmed = Column(BIT, server_default=text("((0))"))
    LastPasswordFailureDate = Column(DateTime)
    PasswordFailuresSinceLastSuccess = Column(Integer, nullable=False, server_default=text("((0                   ))"))
    Password = Column(Unicode(128), nullable=False)
    PasswordChangedDate = Column(DateTime)
    PasswordSalt = Column(Unicode(128), nullable=False)
    PasswordVerificationToken = Column(Unicode(128))
    PasswordVerificationTokenExpirationDate = Column(DateTime)


class WebpagesOAuthMembership(Base):
    __tablename__ = 'webpages_OAuthMembership'

    Provider = Column(Unicode(30), primary_key=True, nullable=False)
    ProviderUserId = Column(Unicode(100), primary_key=True, nullable=False)
    UserId = Column(Integer, nullable=False)


class WebpagesRole(Base):
    __tablename__ = 'webpages_Roles'

    RoleId = Column(Integer, primary_key=True)
    RoleName = Column(Unicode(256), nullable=False, unique=True)

    UserProfile = relationship(u'UserProfile', secondary='webpages_UsersInRoles')


t_webpages_UsersInRoles = Table(
    'webpages_UsersInRoles', metadata,
    Column('UserId', ForeignKey(u'UserProfile.UserId'), primary_key=True, nullable=False),
    Column('RoleId', ForeignKey(u'webpages_Roles.RoleId'), primary_key=True, nullable=False)
)
ningjian@freegodly:~/code/py/django/logcloud$

 

本文转载自:http://www.cnblogs.com/freegodly/p/5132243.html

下一篇: IDEA 16注册
NathanJoy

NathanJoy

粉丝 1
博文 21
码字总数 5090
作品 0
西安
程序员
私信 提问
【转】Django 数据库的操作

class Publisher(models.Model): VALUES WHERE id = 52; FROM book_publisher; 相当于SQL语句:SELECT FROM book_publisherWHERE name = 'NewName'; 缩小范围:Publisher.objects.filter(co......

寂寞的远行者
2012/08/02
2.2K
0
Django基础-Model数据库模型

数据库配置 连接数据库 Django 默认支持sqlite,mysql,oracle,postgresql数据库 - Django默认使用sqlite数据库,自带sqlite数据库驱动,引擎名称:django.db.backends.sqlite3 - mysql数据库引...

ZHAO_JH
2018/08/30
75
0
django model的get和filter方法的区别

django的get和filter方法是django model常用到的,搞清楚两者的区别非常重要。 为了说明它们两者的区别定义2个models class Student(models.Model): name = models.CharField('姓名', max_le......

江北
2014/05/18
8.9K
2
针对 Rails 开发者的 Django 比较

Django 是 Python 最流行的 Web 应用开发框架,很多人认为 Django 就相当于 Python 的 Rails。如果你是一个有经验的 Rails 开发者,那么这篇文章将教会你如何使用 Django 来开发,并且帮助你...

oschina
2012/12/28
4.9K
4
七日Python之路--第十二天(Django Web 开发指南)

《Django Web 开发指南》。貌似使用Django1.0版本,基本内容差不多,细读无妨。地址:http://www.jb51.net/books/76079.html (一)第一部分 入门 (1)内置数字工厂函数 int(12.34)会创建一...

明天以后
2014/08/09
976
0

没有更多内容

加载失败,请刷新页面

加载更多

Python 开发植物大战僵尸游戏

作者:楷楷 链接:https://segmentfault.com/a/1190000019418065 开发思路 完整项目地址: https://github.com/371854496/pygame 更多好玩有趣的python,尽在公众号「Python专栏」,后台回复...

上海小胖
今天
5
0
JVM优化之逃逸分析与分配消除

要了解逃逸分析背后的基本原理,我们先来看下这段有问题的C代码——当然这个是没法用Java来写的: 这段C代码在栈上创建了一个int类型的变量,然后把它的指针作为函数的返回值返回了。这样做是...

onedotdot
今天
3
0
最简单的获取相机拍照的图片

  import android.content.Intent;import android.graphics.Bitmap;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import andr......

MrLins
今天
6
0
说好不哭!数据可视化深度干货,前端开发下一个涨薪点在这里~

随着互联网在各行各业的影响不断深入,数据规模越来越大,各企业也越来越重视数据的价值。作为一家专业的数据智能公司,个推从消息推送服务起家,经过多年的持续耕耘,积累沉淀了海量数据,在...

个推
今天
12
0
第三方支付-返回与回调注意事项

不管是支付宝,微信,还是其它第三方支付,第四方支付,支付机构服务商只要涉及到钱的交易都要进行如下校验,全部成功了才视为成功订单 1.http请求是否成功 2.校验商户号 3.校验订单号及状态...

Shingfi
今天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部