Use response of a method to define a model field in Django

时间:2017-12-18 07:07:06

标签: django postgresql django-models rawsql

I need some dynamic choices fields in a django model and I want to validate if a table exists before get them. I already know how to check if the table exists with raw sql and also I know how to generate my dynamic tuple for choices, but I don't know how to use the response of my validation method. Any ideas?

This is a snippet of my code:

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
    ...

Thanks

0 个答案:

没有答案