简体   繁体   English

来自多个数据表的Django模型

[英]Django model from multiple databes tables

I want to ask if there is a convenient way that allows me to create a django model from multiple tables. 我想问一下是否有一种方便的方法可以让我从多个表创建Django模型。 I am looking for solution, that can be written in similar way: 我正在寻找解决方案,可以采用类似的方式编写:

class ClientTeamContacts(models.Model):
    attribute_1 = models.CharField(db_column='ss',db_table = 'xxx')
    attribute_2 = models.CharField(db_column='cc',db_table = 'yyy')

I will be thankful for every suggestion that can provide some solution. 对于可以提供解决方案的每条建议,我都会感激不尽。

Unfortunately, without hacking the django core, I think the best you can hope for is something like this: 不幸的是,如果不使用django内核,我认为您可以期望的最好的事情是这样的:

class ClientTeamContacts(models.Model):
    attribute_1 = models.ForeignKey('xxx', on_delete=models.CASCADE)
    attribute_2 = models.ForeignKey('yyy', on_delete=models.CASCADE)

This will let you access and modify SS and CC despite them living on a different table. 尽管SS和CC位于不同的表上,这将使您可以访问和修改它们。

record = ClientTeamContacts.objects.get(xxx.ss = 'Smith')
record.xxx.ss = 'Brown'
record.save()

Alternatively, look into using abstract base classes, they abstract out common data and may provide a different way to solve your problem: 或者,研究使用抽象基类,它们抽象出公共数据,并可能提供解决问题的另一种方式:

https://docs.djangoproject.com/en/2.0/topics/db/models/#abstract-base-classes https://docs.djangoproject.com/zh-CN/2.0/topics/db/models/#abstract-base-classes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM