如何创建两个不同django模型的联合?

js5cn81o  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(287)

我有两个django模型

class ModelA(models.Model):
    title = models.CharField(..., db_column='title')
    text_a = models.CharField(..., db_column='text_a')
    other_column = models.CharField(/*...*/ db_column='other_column_a')

class ModelB(models.Model):
    title = models.CharField(..., db_column='title')
    text_a = models.CharField(..., db_column='text_b')
    other_column = None

然后我想使用 union ```
ModelA.objects.all().union(ModelB.objects.all())

但在我看来

(SELECT
model_a.title,
model_a.text_a,
model_a.other_column
FROM model_a)

UNION
(SELECT
model_b.title,
model_b.text_b
FROM model_b)

我当然有例外 `The used SELECT statements have a different number of columns` .
如何创建别名和伪列以使用联合查询?
2uluyalo

2uluyalo1#

您可以在中为最后一列添加注解,以弥补列号不匹配。

a = ModelA.objects.values_list('text_a', 'title', 'other_column')
b = ModelB.objects.values_list('text_a', 'title')
        .annotate(other_column=Value("Placeholder", CharField()))

# for a list of tuples

a.union(b)

# or if you want list of dict

# (this has to be the values of the base query, in this case a)

a.union(b).values('text_a', 'title', 'other_column')
watbbzwu

watbbzwu2#

在django中,联合操作需要具有相同的列,因此 values_list 只能这样使用这些特定列:

qsa = ModelA.objects.all().values('text_a', 'title')
qsb = ModelB.objects.all().values('text_a', 'title')

qsa.union(qsb)

但据我所知,没有办法模仿 NULL 在django联合。所以这里有两种方法。
首先,在模型中添加一个名为 other_column . 您可以将值设置为空,如下所示:

other_column = models.CharField(max_length=255, null=True, default=None)

并使用django queryset联合操作,如本文所述。
最后一个,这个方法有点像Python。试着这样做:

a = ModelA.objects.values_list('text_a', 'title', 'other_column')
b = ModelB.objects.values_list('text_a', 'title')

union_list = list()

for i in range(0, len(a)):
     if b[i] not in a[i]:
         union_list.append(b[i])
     union_list.append(a[i])

希望有帮助!!

dgenwo3n

dgenwo3n3#

在sql查询中,我们可以使用 NULL 定义其余列/别名

(SELECT
`model_a`.`title`,
`model_a`.`text_a`,
`model_a`.`other_column`
 FROM `model_a`)

UNION

(SELECT
`model_b`.`title`,
`model_b`.`text_b`, 
 NULL
 FROM `model_b`)

相关问题