Change the model to inherit from an abstract base class without changing the database

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 minimized, and we have a database with these fields. 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 change the Product class as follows:

 class Product(BaseProduct): # other attributes 

Based on my understanding of abstract base classes, these two settings will create the same tables (right?). So technically, after changing this model, I did not need to make any changes to the database. However, when I try to apply it using the South, he wants to reset the "name" column of the Product table.

Since we already have these tables, I would ideally want to keep the "name" column, and not use other solutions (like OneToOneField).

Thanks!

+4
source share
1 answer

You cannot override model fields with the same name in Django, so the South asks 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 more details.

You may need to export the existing name from each row and map them back to the updated table (possibly using the row identifier as the key).

0
source

Source: https://habr.com/ru/post/1488367/


All Articles