【发布时间】:2018-10-09 11:44:54
【问题描述】:
我在 Django 查询中使用 raw() 和 annotate(),需要一些方法来测试它。
这是我的代码的样子(这已大大简化)
query = """SELECT
table1.id, table1.column1, table1.column2,
table2.other_column1, table2.other_column2
FROM myapp_mymodel as table1
JOIN otherapp_othermodel as table2 ON table1.othermodel_id = table2.id"""
return MyModel.objects.annotate(
other_column1=models.Value('other_column1', models.IntegerField()),
other_column2=models.Value('other_column2', models.DateField())
).raw(query)
用示例数据填充数据库相对简单,但是检查此代码是否返回数据的最佳方法是什么?
在处理标准查询集时有很多选项,但在处理 RawQuerySets 时似乎会消失。
【问题讨论】:
标签: django unit-testing django-queryset django-orm python-unittest