Adding a Field to an Existing Django Model

I have a small site with several users, and I would like to add a field to one of my models (and therefore to the tables.) The problem is that syncdb , of course, will not touch existing models, but I'm trying to figure out how add this field to the model / table without overwriting it all.

Example:

 class Newspaper(models.Model): name = models.CharField() class UserProfile(models.Model): user = models.ForeignKey(user, unique=True) reads = models.ManyToManyField(Newspaper) 

Now I would like to add the URL field ( blank=True ) to the Newspaper model without violating user profiles.

Any ideas?

+50
django-models
May 6 '09 at 3:18 p.m.
source share
3 answers

There is no built-in way to do this. However, there are a number of third-party projects that will do this for you. The two presenters are called South and django-evolution , although there are other others (including the one with which I was associated, dmigrations).

I would definitely recommend South - it has some really neat features and works very well.

+38
May 12, '09 at 19:28
source share

You can try to get SQL for your new field using:

 manage.py sql appname 

Where appname is the name of the application in which your newspaper class lives.

Then you can manually add the field to the appname_newspaper table using ALTER TABLE . I do not know how to do this directly with Django, since Django does not support migrations.

+18
May 6 '09 at 15:31
source share

2015 answer:

Do the following:

 python manage.py makemigrations python manage.py migrate 
+1
Nov 29 '15 at 2:52
source share



All Articles