Django在保持ORM的同时加入两个表

时间:2015-05-06 22:28:07

标签: python mysql django orm connection

使用Django,我试图使用Django从数据库中获取这个特定的结果视图:

select * from CO2_Low_Adj a JOIN CO2_Low_Metrics b on a.gene_id_B = b.gene_id where a.gene_id_A='Traes_1AL_00A8A2030'

我知道我可以使用连接,光标,fetchall来做它并获取字典列表。但是,我想知道在保持ORM的同时是否有办法在Django中执行此操作。

表格如下:

class Co2LowMetrics(models.Model):
    gene_id = models.CharField(primary_key=True, max_length=24)
    modular_k = models.FloatField()
    modular_k_rank = models.IntegerField()
    modular_mean_exp_rank = models.IntegerField()
    module = models.IntegerField()
    k = models.FloatField()
    k_rank = models.IntegerField()
    mean_exp = models.FloatField()
    mean_exp_rank = models.IntegerField()
    gene_gene = models.ForeignKey(Co2LowGene, db_column='Gene_gene_id')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'CO2_Low_Metrics'

class Co2LowGene(models.Model):
    gene_id = models.CharField(primary_key=True, max_length=24)
    entry = models.IntegerField(unique=True)
    gene_gene_id = models.CharField(db_column='Gene_gene_id', max_length=24)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'CO2_Low_Gene'

class Co2LowAdj(models.Model):
    gene_id_a = models.CharField(db_column='gene_id_A', max_length=24)  # Field name made lowercase.
    edge_number = models.IntegerField(unique=True)
    gene_id_b = models.CharField(db_column='gene_id_B', max_length=24)  # Field name made lowercase.
    value = models.FloatField()
    gene_gene_id_a = models.ForeignKey('Co2LowGene', db_column='Gene_gene_id_A')  # Field name made lowercase.
    gene_gene_id_b = models.ForeignKey('Co2LowGene', db_column='Gene_gene_id_B')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'CO2_Low_Adj'

数据库表描述是:

mysql> describe CO2_Low_Metrics;
+-----------------------+-------------+------+-----+---------+-------+
| Field                 | Type        | Null | Key | Default | Extra |
+-----------------------+-------------+------+-----+---------+-------+
| gene_id               | varchar(24) | NO   | PRI | NULL    |       |
| modular_k             | double      | NO   |     | NULL    |       |
| modular_k_rank        | int(8)      | NO   |     | NULL    |       |
| modular_mean_exp_rank | int(8)      | NO   |     | NULL    |       |
| module                | int(8)      | NO   |     | NULL    |       |
| k                     | double      | NO   |     | NULL    |       |
| k_rank                | int(8)      | NO   |     | NULL    |       |
| mean_exp              | double      | NO   |     | NULL    |       |
| mean_exp_rank         | int(8)      | NO   |     | NULL    |       |
| Gene_gene_id          | varchar(24) | NO   | MUL | NULL    |       |
+-----------------------+-------------+------+-----+---------+-------+

mysql> describe CO2_Low_Gene;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| gene_id      | varchar(24) | NO   | PRI | NULL    |                |
| entry        | int(8)      | NO   | UNI | NULL    | auto_increment |
| Gene_gene_id | varchar(24) | NO   |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

mysql> describe CO2_Low_Adj;
+----------------+-------------+------+-----+---------+----------------+
| Field          | Type        | Null | Key | Default | Extra          |
+----------------+-------------+------+-----+---------+----------------+
| gene_id_A      | varchar(24) | NO   | MUL | NULL    |                |
| edge_number    | int(9)      | NO   | PRI | NULL    | auto_increment |
| gene_id_B      | varchar(24) | NO   | MUL | NULL    |                |
| value          | double      | NO   |     | NULL    |                |
| Gene_gene_id_A | varchar(24) | NO   | MUL | NULL    |                |
| Gene_gene_id_B | varchar(24) | NO   | MUL | NULL    |                |
+----------------+-------------+------+-----+---------+----------------+

假设我无法更改底层数据库架构。这可能会改变,如果一个建议可以帮助更容易地使用Django的ORM,那么我可以尝试改变它。

但是,我一直在尝试使用prefetch_related和select_related但是我做错了什么并没有让一切都回来。

使用我的SQL查询,我基本上使用所描述的表CO2_Low_Adj然后CO2_Low_Metrics,其中gene_id_A与gene_gene_id_A('Traes_1AL_00A8A2030')相同,gene_id_B与gene_gene_id_B相同。 SQL查询似乎根本没有使用CO2_Low_Gene。

感谢。

1 个答案:

答案 0 :(得分:0)

Django无法在没有外键的情况下执行JOIN查询。这就是prefetch_relatedselect_related不起作用的原因 - 它们可以处理外键。

我不确定你想要实现什么目标。由于您的gene_id是唯一的,因此只有一个CO2_Low_Metrics实例和一个adj实例列表:

adj = CO2_Low_Adj.objects.filter(gene_id_A='Traes_1AL_00A8A2030') 
metrics = CO2_Low_Metrics.objects.get(pk='Traes_1AL_00A8A2030')

然后单独列出。