How to change type of field in model, when database is already populated ?

467 views
Skip to first unread message

mangu rajpurohit

unread,
May 16, 2015, 9:26:32 AM5/16/15
to django...@googlegroups.com
Hi,

I am new to Django. I am working on an application ,where the types of table fields in database are not known before hand. So, say for eg. In student table, I am assigning CharField to marks field, ie

class Student(models.Model):
     marks = model.CharField(max_length = 3)

and after Student table in database is populated, can I change the type of marks to IntegerField as shown below :-

class Student(models.Model):
     marks = model.IntegerField()


Is it possible ? If not feasible, then what are the alternatives ways/design strategies or what should be done in such cases ? If feasible, then what are its side-effects ?

Sorry for my poor english.

Sergiy Khohlov

unread,
May 16, 2015, 2:43:22 PM5/16/15
to django-users

Dump your data in the SQL file. Of course , separate structure and data. Update structure. Verify ability to switch into digit. If you have using Digit as string in dump you can update data manually or by see or ask. If previous data contains char then no chances.

16 трав. 2015 16:26 "mangu rajpurohit" <rajpuro...@gmail.com> пише:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/aed20dfc-8777-4998-b1ff-224f5012ff20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

donarb

unread,
May 17, 2015, 12:18:30 PM5/17/15
to django...@googlegroups.com
You can use the migration commands in Django to create new columns, then move and possibly convert the data to the new column.



Larry Martell

unread,
May 18, 2015, 10:52:06 AM5/18/15
to django...@googlegroups.com
Why can't you just alter the table:

alter table student modify marks int;

Galia Ladiray

unread,
May 19, 2015, 2:57:13 AM5/19/15
to django...@googlegroups.com
From django 1.7 it is done by the migration
Change your Models class
run makemigrations, it will recognise the change and create a migration file
run migrate command, which will alter the database
Reply all
Reply to author
Forward
0 new messages