Skip to content

Instantly share code, notes, and snippets.

@jarshwah
Created April 19, 2014 10:20
Show Gist options
  • Save jarshwah/11080199 to your computer and use it in GitHub Desktop.
Save jarshwah/11080199 to your computer and use it in GitHub Desktop.
def _get_model(self):
DynamicClass = cache.get('DynamicModel_' + self.table_name)
if DynamicClass is None:
# get the custom columns required for this table..
attrs = dict((c.column_name, models.CharField(max_length=100, blank=True, null=True)) for c in self.custom_fields.all())
class Meta:
db_table = self.table_name
app_label = self._meta.app_label
managed = False
unique_together = (('chain_id', 'chain_n', 'group_id'),)
attrs['Meta'] = Meta
attrs['__module__'] = ThisApp.__module__
# create then cache it
DynamicClass = type(str(self.table_name), (CallbackRecord,), attrs)
cache.set('DynamicModel_' + self.table_name, DynamicClass, 60*20)
return DynamicClass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment