I used django's inspectdb
function to import an existing database as django models. The output is below
The tables are dynamically created by an external program, partitioned by month or week (depending on the data model, these ones are month).
What I want, is to have the single django model, but be able to change the table it uses for them at runtime, so that I can set which table it pulls from.
Am I able to do something like set a callback instead of a string to get the table name?
def get_history_partition_name(timestamp):
return 'formatted_table_name_from_timestamp'
class SqltData1202211(models.Model):
tagid = models.IntegerField(primary_key=True)
intvalue = models.BigIntegerField(blank=True, null=True)
floatvalue = models.FloatField(blank=True, null=True)
stringvalue = models.CharField(max_length=255, blank=True, null=True)
datevalue = models.DateTimeField(blank=True, null=True)
dataintegrity = models.IntegerField(blank=True, null=True)
t_stamp = models.BigIntegerField()
class Meta:
managed = False
#db_table = 'sqlt_data_1_2022_11'
db_table = get_history_partition_name # Example of a callback instead of a string
unique_together = (('tagid', 't_stamp'),)
class SqltData1202302(models.Model):
tagid = models.IntegerField(primary_key=True)
intvalue = models.BigIntegerField(blank=True, null=True)
floatvalue = models.FloatField(blank=True, null=True)
stringvalue = models.CharField(max_length=255, blank=True, null=True)
datevalue = models.DateTimeField(blank=True, null=True)
dataintegrity = models.IntegerField(blank=True, null=True)
t_stamp = models.BigIntegerField()
class Meta:
managed = False
db_table = 'sqlt_data_1_2023_02'
unique_together = (('tagid', 't_stamp'),)