Changing Django data type in models without dropping a table

I created a Django application. Now I wanted to change the field type for 1 of my db field in models. Since there are some entries in the database with the current type, I think it cannot be changed just by changing it in models.py. From googling, I found out that it can be changed without dropping tables, changing it through the sql console. But, being new to Django, I cannot do this. Can someone help me do this? These are my models.py

class EmployeeDetails(models.Model): userName = models.CharField(max_length=200) designation = models.CharField(max_length=200) employeeID = models.IntegerField() contactNumber = models.CharField(max_length=200) project = models.CharField(max_length=200) dateOfJoin=models.TextField() 

Now I want to change dateOfJoin = models.DateField (). How can I change TextFieild to DateField without dropping the table using sql console commands?

+4
source share
3 answers

I earned it differently. I selected my database via sql console and added new columns with sql query. And it worked just fine :) Thanks a lot for the help the guys gave you.

0
source

You need the South app
Here are the steps (see the document for details):

 ./manage.py convert_to_south yourapp 

this will create a migration folder and fake the first migration

Then add new_dateOfJoin DateField to models.py:

 ./manage.py migrate yourapp 

this will create and apply the migration immediately (named 0002 _...) to add this field to db

Create a datamigration 0003 file in which you simply show your text value and save it in a new field. Then apply this migration:

 ./manage.py migrate yourapp 

Modify the models.py file to comment on the old date field and perform the migration again (this will create the migration file 0004 _....)

The last step is to rename the db field, so you do not need to modify the existing code:

 ./manage.py schemamigration yourapp rename_dateofjoin --empty 

This will create an empty schema migration file named 0005_rename_dateofjoin
Manually modify this file 0005 as described here, but use db.rename_column('app_foo', 'name', 'full_name') , as in this other example .

Dropping the entire table instead of these 5 steps may be easier, but if you have several instances of the application (for example, a production production server and a development machine), you need such tools.
With a better understanding of the South, this could be done in less than 5 steps, but since migration can be difficult to destroy or reproduce sometimes, I prefer to have different steps ...

+18
source

The South project is typically used to transfer schemes and data. I am not sure if it can be used to convert a text field to a date field, but I will not be surprised if it is possible.

+2
source

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


All Articles