Skip to content

Commit

Permalink
Fixed table naming system for newer Django releases and made it respe…
Browse files Browse the repository at this point in the history
…ct custom naming
  • Loading branch information
wolph committed Nov 17, 2019
1 parent 51692a6 commit d892c29
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions django_utils/base_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ class ModelBaseMeta(base.ModelBase):
'''

def __new__(cls, name, bases, attrs):
class_ = base.ModelBase.__new__(cls, name, bases, attrs)
module_name = formatters.camel_to_underscore(name)

app_label = class_.__module__.split('.')[-2]
db_table = '%s_%s' % (app_label, module_name)
if not getattr(class_._meta, 'proxy', False):
class_._meta.db_table = db_table

return class_

# Get or create Meta
if 'Meta' in attrs:
Meta = attrs['Meta']
else:
Meta = type('Meta', (object,), dict(
__module__=attrs['__module__'],
))
attrs['Meta'] = Meta

# Override table name only if not explicitly defined
if not hasattr(Meta, 'db_table'):
module_name = formatters.camel_to_underscore(name)
app_label = attrs['__module__'].split('.')[-2]
Meta.db_table = '%s_%s' % (app_label, module_name)

return base.ModelBase.__new__(cls, name, bases, attrs)


class ModelBase(six.with_metaclass(ModelBaseMeta, models.Model)):
class Meta:
Expand Down

0 comments on commit d892c29

Please sign in to comment.