0

I want to create many models based on an abstract one, but with different default values for the fields.

def get_model(name):

    class AbstractModel(models.Model):
        field1 = models.CharField(max_length=100, default=name)

        class Meta:
            abstract = True

    return AbstractModel

class MyModel(get_model('MyModel')):
    pass

What are disadvantages of the approach presented above? Is there any better way?

  • This sounds like your DB design could be improved. Why would you need different tables/models just to have a different default value? – Nils Werner Nov 08 '16 at 12:32
  • I have 10 Products. Each of them have name, creation date, foreign key to maintainer etc. On top of that, each of them have non-generic fields. Product Car have safety_level, Product Book has number_of_pages and author, Product Chair has wood_type and country_origin etc. – LoremIpsum Nov 08 '16 at 12:40

1 Answers1

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'
pt12lol
  • 2,332
  • 1
  • 22
  • 48
  • Note I presented only a simple example. In real world, each of these models would have 5-10 additional, non-generic fields. Could you elaborate on "lots of disadvantages"? – LoremIpsum Nov 08 '16 at 12:38
  • The two I elaborated on are most important. I am almost certain other cons will appear. Why this is such important for you to use this weird function over normal inheritance or composition? Inheritance or composition are good in this case imho. – pt12lol Nov 08 '16 at 13:14
  • Normal inheritance won't let me change default values – LoremIpsum Nov 08 '16 at 13:28
  • Have you tried to overwrite super `field1` or even its default value? – pt12lol Nov 08 '16 at 13:49
  • Overwriting it means I have redefine the whole field - all the other parameters like max_length, null, blank, unique, index_db etc. – LoremIpsum Nov 08 '16 at 14:18
  • Why not overwrite only default value of the field? – pt12lol Nov 08 '16 at 14:20
  • How would you do that? Could you provide an example? Edit: It's not possible http://stackoverflow.com/q/2344751/7126643 – LoremIpsum Nov 08 '16 at 14:29
  • @pt12lol: remember to run original constructor in your overriden one. – d33tah Nov 08 '16 at 23:58
  • @d33tah: of course, I just wanted to provide as simple case as it is possible – pt12lol Nov 09 '16 at 06:50
  • @pt12lol: it's just that behavior might change if you don't call it – d33tah Nov 09 '16 at 10:04
  • @d33tah: what way it might change? you will start with running `A` constructor which assign default value to `f` specific for `A` class, and then it will be overwritten by class `B` constructor content. There is lots of solutions for this problem, but my snippet just proves it is possible. – pt12lol Nov 09 '16 at 10:17