Search code examples
djangoinheritancemodelinsertion

django model inheritance - multi table - manual insertion


I just redesigned a part of my DB to use multi table inheritance and things are much much easier now!

I do have one question, I am trying to manually insert thousands of rows in my tables and now I cannot figure out how to do it.

e.g.: Before:

class personal(models.Model):
    who = models.CharField...
    stamp = models.DateTime...
    title = models.CharField ...
    descr = models.CharField ...

and in python shell:

for i in range(1000):
    t=datetime.now()
    e=timedelta(minutes=i)
    entry = personal(who='xyz', stamp=t+e, title='title '+str(i), descr='description for '+str(i)
    entry.save()

After:

class common(models.Model):
    who ...
    stamp ...

class personal(common):
    title ...
    descr ...

and in python shell if I do same thing as above it says personal doesnt have a column named who/stamp

my question: how can I adjust above 'for' to insert multiple rows in my new db?

thanks!


Solution

  • You probably want abstract model inheritance:

    class common(models.Model):
        who ...
        stamp ...
    
        class Meta:
            abstract = True
    
    class personal(common):
        title ...
        descr ...
    

    Then above code should work.

    EDIT:

    For non-abstract inheritance, the insert code should be something like this:

    for i in range(1000):
        t=datetime.now()
        e=timedelta(minutes=i)
        base = common.objects.create(who='xzy', stamp=t+e)
        entry = personal.objects.create(common_ptr=base, title='title '+str(i), descr='description for '+str(i)