Young87

SmartCat's Blog

So happy to code my life!

游戏开发交流QQ群号60398951

当前位置:首页 >跨站数据

获取postgres数据库所有表名

  • 获取postgres数据库的所有表名

        def get(self):
            try:
                tables = connection.introspection.table_names()
                return JsonRes(tables, success=True, info="get database name Successfully.")
            except Exception as err:
                return JsonRes(None, success=False, info='get database name failed, please check the format.')
    

    最后tables=[‘table1’, ‘table2’]

  • 获取postgres数据库的一个表的所有column

    def get(self, request, tablename):
        try:
            with connection.cursor() as cursor:
                desc = connection.introspection.get_table_description(cursor, tablename)
                columns = [r[0] for r in desc]
            return JsonRes(columns, success=True, info="get column name Successfully.")
        except Exception as err:
            return JsonRes(None, success=False, info='get column name failed.')

这里区分以下flask和django下module的区别
django

# 从django引入module
from django.db import models

class USER(models.Model):

    name = models.CharField(db_index=True, max_length=200, null=False, default='')
    description = models.CharField(max_length=255, null=False, default='')
    teacher_id= models.ForeignKey('TEACHER', on_delete=models.CASCADE, default=1)
	# teacher_id作为user的外键
  
    class Meta:
        db_table = 'user'
        # 数据库名

    def __str__(self):
        return self.name

flask

from sqlalchemy import Column, String, ForeignKey

from cxcomm.db.model import BaseTable, HasId, HasTime


class USER(BaseTable, HasId, HasTime):
 
    __tablename__ = 'user'

    name = Column(String(255), nullable=False)
	description = Column(String(255), nullable=False)
    teacher_id= Column(String(36), ForeignKey('teacher.id'), nullable=True, index=True)

from yactrl.models.user import USER
# 导入之前的模型
from cxcomm.db.ormobject import ORMBaseObject
from cxcomm.db import fields


class USERObj(ORMBaseObject):
    """snmp object
    """
    db_model = USER
    fields = {
        'id': fields.UUIDField(nullable=True),
        'name ': fields.StringField(nullable=False),
        'description ': fields.UUIDField(nullable=False),
        'teacher_id': fields.StringField(nullable=True)
    }
    allow_update_key = ['description ']

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: QT初学者(随笔记录)

下一篇: ES6之Set对象详解

精华推荐