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

      <small id='2repx'></small><noframes id='2repx'>

      <legend id='2repx'><style id='2repx'><dir id='2repx'><q id='2repx'></q></dir></style></legend>
        <bdo id='2repx'></bdo><ul id='2repx'></ul>
      1. 单个 Django 模型,多个表?

        时间:2023-06-25

        <small id='59qXb'></small><noframes id='59qXb'>

          1. <i id='59qXb'><tr id='59qXb'><dt id='59qXb'><q id='59qXb'><span id='59qXb'><b id='59qXb'><form id='59qXb'><ins id='59qXb'></ins><ul id='59qXb'></ul><sub id='59qXb'></sub></form><legend id='59qXb'></legend><bdo id='59qXb'><pre id='59qXb'><center id='59qXb'></center></pre></bdo></b><th id='59qXb'></th></span></q></dt></tr></i><div id='59qXb'><tfoot id='59qXb'></tfoot><dl id='59qXb'><fieldset id='59qXb'></fieldset></dl></div>
              <bdo id='59qXb'></bdo><ul id='59qXb'></ul>
              <legend id='59qXb'><style id='59qXb'><dir id='59qXb'><q id='59qXb'></q></dir></style></legend>
                  <tbody id='59qXb'></tbody>

                  <tfoot id='59qXb'></tfoot>
                  本文介绍了单个 Django 模型,多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 MySQL 数据库中有几个临时表,它们共享相同的架构并具有动态名称.我将如何使用 Django 与这些表进行交互?单个模型可以从多个表中提取数据吗?

                  解决方案

                  我相信,您可以创建一个工厂函数,该函数将返回您的模型的动态 db_table.

                  def getModel(db_table):类 MyClass(models.Model):# 像往常一样定义...元类:db_table = db_table返回我的课堂newClass = getModel('29345794_table')newClass.objects.filter( ...

                  每次调用此函数时,Django 不会创建类的 _meta 属性的新实例.为 _meta 创建一个新实例,它依赖于类的名称(Django 必须将它缓存在某处).元类可用于在运行时更改类的名称:

                  def getModel(db_table):类 MyClassMetaclass(models.base.ModelBase):def __new__(cls, name, bases, attrs):名称 += db_table返回models.base.ModelBase.__new__(cls, name, bases, attrs)类 MyClass(models.Model):__metaclass__ = MyClassMetaclass元类:db_table = db_table返回我的课堂

                  不确定是否可以在已定义的类上动态设置.我自己没有这样做过,但它可能会奏效.

                  您可以随时设置.

                  <预><代码>>>>MyModel._meta.db_table = '10293847_table'>>>MyModel.objects.all()

                  I have several temporary tables in a MySQL database that share the same schema and have dynamic names. How would I use Django to interface with those tables? Can a single model draw data from multiple tables?

                  解决方案

                  You could, I believe, make a factory function that would return your model with a dynamic db_table.

                  def getModel(db_table):
                    class MyClass(models.Model):
                       # define as usual ...
                       class Meta:
                         db_table = db_table
                  
                    return MyClass
                  
                  newClass = getModel('29345794_table')
                  newClass.objects.filter( ...
                  

                  EDIT: Django does not create a new instance of the class's _meta attribute each time this function is called. Creating a new instance for _meta it is dependent upon the name of the class (Django must cache it somewhere). A metaclass can be used to change the name of the class at runtime:

                  def getModel(db_table):
                    class MyClassMetaclass(models.base.ModelBase):
                      def __new__(cls, name, bases, attrs):
                        name += db_table
                        return models.base.ModelBase.__new__(cls, name, bases, attrs)
                  
                    class MyClass(models.Model):
                      __metaclass__ = MyClassMetaclass
                  
                      class Meta:
                        db_table = db_table
                  
                    return MyClass
                  

                  not sure if it can be set dynamically on an already-defined class. I haven't done this myself but it might work.

                  You can set this whenever.

                  >>> MyModel._meta.db_table = '10293847_table'
                  >>> MyModel.objects.all()
                  

                  这篇关于单个 Django 模型,多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 MySQL 中实现关键字搜索? 下一篇:MySQL 中的 BLOB 和 TEXT 数据类型有什么区别?

                  相关文章

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

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