Frage

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!

War es hilfreich?

Lösung

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).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top