0

我需要 django 模型中的一些动态选择字段,并且我想在获取它们之前验证表是否存在。我已经知道如何使用原始 sql 检查表是否存在,并且我知道如何为选择生成动态元组,但我不知道如何使用我的验证方法的响应。有任何想法吗?

这是我的代码片段:

class SomeModel(models.Model):
    ...
    row = False

    def check_table(self):
        with connection.cursor() as cursor:
            cursor.execute(
                "SELECT EXISTS("
                "SELECT *"
                " FROM pg_tables WHERE schemaname = \'schema_name\' AND "
                "tablename = \'table_name\')")
            row = cursor.fetchall()
        return row
    # Then, I need to put that reponse in a variable to do something like this.
    if table_exists:
        # do something
    ...

谢谢

4

0 回答 0