Django: convert CharField to TextField with intact data

Is there a way to change CharField to TextField and keep the data from this column intact?

Now I have the following:

class TestLog(models.Model): failed_reqs = models.CharField(max_length=DB_MAX_CHAR_LENGTH, blank=True) passed_reqs = models.CharField(max_length=DB_MAX_CHAR_LENGTH, blank=True) 

But DB_MAX_CHAR_LENGTH is 500, and as it turns out, this field can sometimes exceed this, so I want to go to:

 class TestLog(models.Model): failed_reqs = models.TextField(blank=True) passed_reqs = models.TextField(blank=True) 

How can I do this and save my data in the production database?

Djangobook details how to add and remove fields, and I tried to use the South to do this, but the South gave me an error, and looking at the result before the error, it seems that the South is falling and recreating the database. Is that what south data transfer is all about?

+8
python django
source share
3 answers

Assuming you have access to the database, since you mentioned how Django talks about adding and removing columns, I listed the methods for both Postgresql and MySQL, since you did not indicate what you were using.

 Postgresql: BEGIN; ALTER TABLE "TestLog" ALTER COLUMN "failed_reqs" TYPE TEXT, ALTER COLUMN "passed_reqs" TYPE TEXT; COMMIT; MySQL: BEGIN; ALTER TABLE TestLog MODIFY failed_reqs TEXT NULL, MODIFY passed_reqs TEXT NULL; COMMIT; 

I would highly recommend backing up the database before making these changes.

+6
source share

Go ahead, I would continue to study the South approach. This is a workflow that I would try:

1) Transition the southern schema to create two new TextField fields called "fail_reqs_txt" and "pass_reqs_txt".

2) Create a data migration to transfer data from old fields to new fields

3) Create a schema migration to remove the original fields "failed_reqs" and "pass_reqs".

---- If you need the fields to be the same name as the original, I would go to:

4) Create a schema migration to add "failed_reqs" and "pass_reqs" as TextFields

5) Create a data transfer to transfer from "failed_reqs_txt" and "pass_reqs_txt" to the fields "failed_reqs" and "pass_reqs".

6) Create a schema migration to remove the failed_reqs_txt and pass_reqs_txt fields.

Despite the fact that there are many transitions, it breaks each change into atomic migrations. I would try this first. I am not sure why the South will fall and recreate dB. Did you use the convert_to_south parameter when adding south to your project? I think this replaces migration and allows the south to know that it is working with an existing project, not a new one.

Alternatively, you can do some direct ALTERS in the database to change the column type, and then upgrade model.py from CharField to TextField. Postgres supposedly supports implicitly changing data types in this way. (See Section 5.5.6.) I'm not sure about mysql, but I think it works the same. (CharField to TextFiled must be a compatible conversion)

Despite this, I backed up my data before making any changes. Hope this helps.

+5
source share

This is solved by migrating django (1.7+). If you change the type from CharField to TextField, AlterField is migrated, which does the right thing.

+5
source share

All Articles