单个Django模型,多个表? [英] Single Django model, multiple tables?

查看:153
本文介绍了单个Django模型,多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个完全相同的问题已经被问过,但是我希望得到一个更好的答案(这不涉及在运行时修改类属性)。这是一个问题:



单个Django模型,多个表?



我有同样的问题 - 我正在寻找类似于回复者的第一个回复的解决方案,但实际上是有效的。如果没有更好的解决方案,任何人都可以评论应答者解决方案的可靠性?在我看来,更改数据库名称和查询数据库之间的延迟可能会从错误的表中返回结果:



查询1:更改名称



查询2:再次更改名称



查询1:获取结果(但使用错误的名称从查询2)



编辑:该模型旨在用于〜15个表 - 因此继承是不切实际的,因为每次都需要一个新的模型名称。



谢谢



PS对于一个动态表和数据库可互换的问题,如果这不是正确的方式来解决问题,我很抱歉。

解决方案

您可能使用简单的方法或属性,比您所链接的问题更具性感的行为模式:

  import copy 

class MyModel(models.Model):
#anything
@property
def table_name(self):
return self._meta.db_table

@ table_name.setter
def table_name(self,value):
new_meta = copy.copy(self._meta)
new_meta.db_table = value
self._meta = new_meta

@classmethod
def set_qs_for_table(cls,qs,table):
my_class = copy.copy(cls)
my_options = copy.copy(my_class._meta )
my_class._meta = my_options
qs.model = my_class

您可以尝试这样的东西...



复制部分是为了避免模型之间共享选项的危险。我花了时间找到这部分解决方案。但是对于其余的,它看起来性感和直接。



当然,一旦在python代码中,你可以使用

  qs = MyClass.objects.all()
MyClass.set_qs_for_table(qs,this_table)
my_instance = qs [0]
my_instance.table_name = $
my_instance.save(using =this_db)


I know this exact same question has been previously asked, but I was hoping for a 'better' answer (that does not involve modifying class attributes at runtime). This was the question:

Single Django model, multiple tables?

I have the same problem - I am looking for a solution similar to the responder's first reply, but that actually works. If there is no better solution, can anyone comment on how reliable the responder's solution is? It seems to me that the delay between changing the database name and querying the database could end up returning results from the wrong table:

query 1: change the name

query 2: change the name again

query 1: get results (but using the incorrect name from query 2)

Edit: The model is intended for use on ~15 tables - so inheritance is impractical, since it would require a new model name every time.

Thanks

P.S. My apologies if this is not the correct way of asking for an elaboration on a question.

解决方案

for a dynamic table and database interhangeable model with a more sexy behaviour than the one in your linked question you may use simple methods or properties:

import copy

class MyModel(models.Model):
    # anything
    @property
    def table_name(self):
        return self._meta.db_table

    @table_name.setter
    def table_name(self, value):
        new_meta = copy.copy(self._meta)
        new_meta.db_table = value
        self._meta = new_meta

    @classmethod
    def set_qs_for_table(cls, qs, table):
        my_class = copy.copy(cls)
        my_options = copy.copy(my_class._meta)
        my_class._meta = my_options
        qs.model = my_class

You may try something like this...

The copy part is to avoid danger of shared options between models. It took me time to find this part of the solution. but for the rest it looks sexy and straightforward.

Of course once in python code you may use

qs = MyClass.objects.all()
MyClass.set_qs_for_table(qs, "this_table")
my_instance = qs[0]
my_instance.table_name = "that_table"
my_instance.save(using="this_db")

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

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