[Fixed]-Dynamically creating models in Django

1👍

It looks like you need a single model instead of set of models. You might have a single model with an additional field having some kind of model type – this would be exactly the same like your model name.

There is lots of disadvantages of your idea. Mainly this will be very confusing for the others and you influence negatively for performance due to constant using code reflections.

UPDATE

Default value name dependent on the class you can make by overwriting it in a constructor:

from django.db import models


class A(models.Model):
    def __init__(self):
        self.f = 'model A name'
    f = models.CharField(max_length=100)

class B(A):
    def __init__(self):
        self.f = 'model B name'

Then try it in an interactive session:

>>> from polls import models
>>> models.A().f
'model A name'
>>> models.B().f
'model B name'

Leave a comment