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