1

Currently, most of my models look like this:

class A(models.Model):
    # model attributes
    class Meta:
        db_table = 'A'

class B(models.Model):
    # model attributes
    class Meta:
        db_table = 'B'

Is there a way to do this automatically? I tried adding the meta class after defining the class, but because of how Django handles Meta classes for models, this doesn't work. Am I just stuck defining the Meta classes by hand?

Samuel Barr
  • 434
  • 4
  • 12
  • To automate what? Naming the database table exactly like the class name? – Willem Van Onsem Jul 18 '18 at 19:42
  • @willem Let's say that A and B were defined in an app named "myapp". Then the default table name would be "myapp_a" - I would like it to somehow default to "A" – Samuel Barr Jul 18 '18 at 19:45
  • I think this answered to your question: https://stackoverflow.com/questions/27175106/renaming-modelstables-in-django – Wariored Jul 18 '18 at 20:35

1 Answers1

1

Not sure why you want to do that but you could do something like:

for model in models:
    model._meta.db_table = model.__class__.__name__
Cyrlop
  • 1,894
  • 1
  • 17
  • 31