4

I have a simple model for a product that looks like this:

class Product(models.Model):
    name = models.CharField(max_length=80)
    # other attributes

We already have this rolled out, and have a DB with these fields filled out. I want to change this model to inherit from a base class, that looks like this:

class BaseProduct(models.Model):
    name = models.CharField(max_length=80)

    class Meta(object):
         abstract = True

And modify the Product class like so:

class Product(BaseProduct):
    # other attributes

Based on my understanding of Abstract Base Classes, these two setups will create the same tables (right?). So technically, after changing this model, I shouldn't have to do any modifications in the database. However, when I try to apply it using South, it wants to drop the 'name' column of the Product table.

Since we already have these tables rolled out, I would ideally like to keep the 'name' column, as opposed to using other solutions (like a OneToOneField).

Thanks!

user1496984
  • 10,957
  • 8
  • 37
  • 46
  • Do you want to keep the same columns and only override behavior? – J0HN Jun 26 '13 at 20:21
  • That code looks fine to me just for Django - does the output of `manage.py sql` look the same in both versions? I suspect something getting confused in South. Possibly related: http://stackoverflow.com/questions/7108899/using-django-south-to-move-from-concrete-inheritance-to-abstract-inheritance – Peter DeGlopper Jun 26 '13 at 20:28
  • show me the generated schememigration – Leandro Jun 26 '13 at 20:33
  • What versions of Django and South are you using? Also, what exactly are you doing when you "try to apply it using South"? If you make a change like the one you're describing and run `manage.py schemamigration --auto` South should say "Nothing seems to have changed" but if does generate a migration can you post the `forwards` method of that migration for us? – Kevin Jun 27 '13 at 22:38

1 Answers1

0

You cannot override model fields of the same name in Django, which is why South is asking you to remove the 'name' field from the child class. See https://docs.djangoproject.com/en/dev/topics/db/models/#field-name-hiding-is-not-permitted for further details.

You may need to export the existing name from each row and map them back into the updated table (perhaps by using row id as the key).

chewynougat
  • 1,099
  • 2
  • 11
  • 19