<tfoot id='1CqJS'></tfoot>

    <i id='1CqJS'><tr id='1CqJS'><dt id='1CqJS'><q id='1CqJS'><span id='1CqJS'><b id='1CqJS'><form id='1CqJS'><ins id='1CqJS'></ins><ul id='1CqJS'></ul><sub id='1CqJS'></sub></form><legend id='1CqJS'></legend><bdo id='1CqJS'><pre id='1CqJS'><center id='1CqJS'></center></pre></bdo></b><th id='1CqJS'></th></span></q></dt></tr></i><div id='1CqJS'><tfoot id='1CqJS'></tfoot><dl id='1CqJS'><fieldset id='1CqJS'></fieldset></dl></div>

    • <bdo id='1CqJS'></bdo><ul id='1CqJS'></ul>
  • <legend id='1CqJS'><style id='1CqJS'><dir id='1CqJS'><q id='1CqJS'></q></dir></style></legend>
    1. <small id='1CqJS'></small><noframes id='1CqJS'>

        SQLAlchemy:从表名中获取模型.据我所知,这可能意味着将一些函数附加到元类构造函数

        SQLAlchemy: get Model from table name. This may imply appending some function to a metaclass constructor as far as I can see(SQLAlchemy:从表名中获取模型.据我所知,这可能意味着将一些函数附加到元类构造函数)
      1. <legend id='cnndS'><style id='cnndS'><dir id='cnndS'><q id='cnndS'></q></dir></style></legend>

          <tfoot id='cnndS'></tfoot>

            <small id='cnndS'></small><noframes id='cnndS'>

              <i id='cnndS'><tr id='cnndS'><dt id='cnndS'><q id='cnndS'><span id='cnndS'><b id='cnndS'><form id='cnndS'><ins id='cnndS'></ins><ul id='cnndS'></ul><sub id='cnndS'></sub></form><legend id='cnndS'></legend><bdo id='cnndS'><pre id='cnndS'><center id='cnndS'></center></pre></bdo></b><th id='cnndS'></th></span></q></dt></tr></i><div id='cnndS'><tfoot id='cnndS'></tfoot><dl id='cnndS'><fieldset id='cnndS'></fieldset></dl></div>
                <bdo id='cnndS'></bdo><ul id='cnndS'></ul>
                    <tbody id='cnndS'></tbody>
                1. 本文介绍了SQLAlchemy:从表名中获取模型.据我所知,这可能意味着将一些函数附加到元类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想创建一个函数,给定一个表名,返回具有该表名的模型.例如:

                  I want to make a function that, given the name of a table, returns the model with that tablename. Eg:

                  class Model(Base):
                      __tablename__ = 'table'
                      ...a bunch of Columns
                  
                  def getModelFromTableName(tablename):
                     ...something magical
                  

                  所以 getModelFromTableName('table') 应该返回 Model 类.

                  so getModelFromTableName('table') should return the Model class.

                  我的目标是在我正在制作的简单表单生成器中使用该函数,因为 FormAlchemy 不适用于 python3.2,我希望它能够很好地处理外键.

                  My aim is to use the function in a simple form generator I'm making since FormAlchemy does not work with python3.2 and I want it to handle foreign keys nicely.

                  谁能告诉我如何让 getModelFromTableName 工作?

                  Can anyone give me any pointers on how to get getModelFromTableName to work?

                  这是我的一个想法(这可能是完全错误的,我以前没有使用过元类......)

                  Here's one idea I have (it might be totally wrong, I haven't worked with meta classes before...)

                  如果我要让我的模型类继承自 Base 以及其他一些类 (TableReg) 并让 TableReg 的类元存储 Model.tablename 在某个全局字典或 Singleton 中.

                  What if I were to make my Model classes inherit from Base as well as some other class (TableReg) and have the class meta of TableReg store Model.tablename in some global dictionary or Singleton.

                  我意识到这可能完全关闭,因为 Base 的元类做了一些我不想破坏的非常重要且非常漂亮的东西,但我认为必须有一种方法可以让我附加一点构造函数代码我的模型的元类.或者我不明白.

                  I realise this could be totally off because Base's metaclass does some very important and totally nifty stuff that I don't want to break, but I assume there has to be a way for me to append a little bit of constructor code to the meta class of my models. Or I don't understand.

                  推荐答案

                  灵感来自 Eevee 的评论:

                  def get_class_by_tablename(tablename):
                    """Return class reference mapped to table.
                  
                    :param tablename: String with name of table.
                    :return: Class reference or None.
                    """
                    for c in Base._decl_class_registry.values():
                      if hasattr(c, '__tablename__') and c.__tablename__ == tablename:
                        return c
                  

                  这篇关于SQLAlchemy:从表名中获取模型.据我所知,这可能意味着将一些函数附加到元类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                2. <legend id='r4VrF'><style id='r4VrF'><dir id='r4VrF'><q id='r4VrF'></q></dir></style></legend>

                    <i id='r4VrF'><tr id='r4VrF'><dt id='r4VrF'><q id='r4VrF'><span id='r4VrF'><b id='r4VrF'><form id='r4VrF'><ins id='r4VrF'></ins><ul id='r4VrF'></ul><sub id='r4VrF'></sub></form><legend id='r4VrF'></legend><bdo id='r4VrF'><pre id='r4VrF'><center id='r4VrF'></center></pre></bdo></b><th id='r4VrF'></th></span></q></dt></tr></i><div id='r4VrF'><tfoot id='r4VrF'></tfoot><dl id='r4VrF'><fieldset id='r4VrF'></fieldset></dl></div>

                      <bdo id='r4VrF'></bdo><ul id='r4VrF'></ul>
                    • <tfoot id='r4VrF'></tfoot>

                      <small id='r4VrF'></small><noframes id='r4VrF'>

                          <tbody id='r4VrF'></tbody>