Can I change the models.py of an existing django app?

150 views
Skip to first unread message

Zeynel

unread,
Dec 11, 2009, 12:39:07 PM12/11/09
to Django users
I just want to add 2 new fields to the model. If I upload the new
models.py would the app work as before? I asked the same question in
OS that includes the models. Thanks for the help.

http://stackoverflow.com/questions/1889622/how-to-replace-models-py-in-django-app-in-the-production-server

mail....@gmail.com

unread,
Dec 11, 2009, 12:50:26 PM12/11/09
to Django users
On Dec 11, 11:39 am, Zeynel <azeyn...@gmail.com> wrote:
> I just want to add 2 new fields to the model. If I upload the new
> models.py would the app work as before?

Yes, but you'll have to manually adjust your database to have the new
fields.

Phlip

unread,
Dec 11, 2009, 1:21:31 PM12/11/09
to Django users
> > I just want to add 2 new fields to the model. If I upload the new
> > models.py would the app work as before?
>
> Yes, but you'll have to manually adjust your database to have the new
> fields.

What does manage.py syncdb do?

Zeynel

unread,
Dec 11, 2009, 1:21:41 PM12/11/09
to Django users
Ok. What else do I need to do after uploading the new models.py with
new fields to the server and run manage.py syncdb?


On Dec 11, 12:50 pm, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:

Zeynel

unread,
Dec 11, 2009, 1:28:38 PM12/11/09
to Django users
On Dec 11, 1:21 pm, Phlip <phlip2...@gmail.com> wrote:

> What does manage.py syncdb do?

I think it sets up the new database:
http://stackoverflow.com/questions/1889622/how-to-replace-models-py-in-django-app-in-the-production-server/1889650#1889650

mail....@gmail.com

unread,
Dec 11, 2009, 1:52:43 PM12/11/09
to Django users
On Dec 11, 12:21 pm, Zeynel <azeyn...@gmail.com> wrote:
> Ok. What else do I need to do after uploading the new models.py with
> new fields to the server and run manage.py syncdb?
>

syncdb will not alter an existing database. You can run it (it won't
hurt) but it won't insert the new fields. You'll need to go to your
database, either through the command line or through a tool like
phpmyadmin and add the new fields manually. You can, however, run
manage.py sql YOURAPP to see what the table should look like.

Zeynel

unread,
Dec 11, 2009, 2:36:34 PM12/11/09
to Django users
On Dec 11, 1:52 pm, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:
> On Dec 11, 12:21 pm, Zeynel <azeyn...@gmail.com> wrote:
>
> syncdb will not alter an existing database. You can run it (it won't
> hurt) but it won't insert the new fields.

I am confused about this because the django documentation here

http://docs.djangoproject.com/en/dev/intro/tutorial01/#activating-models

says that,

>> Now, run syncdb again to create those model tables in your database:
>> python manage.py syncdb
>> The syncdb command runs the sql from 'sqlall' on your database
>> for all apps in INSTALLED_APPS that don't already exist in your database.
>> This creates all the tables, initial data and indexes for any apps
>> you have added to your project since the last time you ran syncdb.
>> syncdb can be called as often as you like,
>> and it will only ever create the tables that don't exist.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

So, it seems that, according to documentation, syncdb will create the
new tables?

I am using sqlit3, by the way.

mail....@gmail.com

unread,
Dec 11, 2009, 2:42:43 PM12/11/09
to Django users
On Dec 11, 1:36 pm, Zeynel <azeyn...@gmail.com> wrote:
> I am confused about this because the django documentation here
>
> http://docs.djangoproject.com/en/dev/intro/tutorial01/#activating-models
>
> says that,
>
> >> Now, run syncdb again to create those model tables in your database:
> >> python manage.py syncdb
> >> The syncdb command runs the sql from 'sqlall' on your database
> >> for all apps in INSTALLED_APPS that don't already exist in your database.
> >> This creates all the tables, initial data and indexes for any apps
> >> you have added to your project since the last time you ran syncdb.
> >> syncdb can be called as often as you like,
> >> and it will only ever create the tables that don't exist.
>
>                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> So, it seems that, according to documentation, syncdb will create the
> new tables?
>
> I am using sqlit3, by the way.

No it will "create those model tables in your database" ... "for all
apps in INSTALLED_APPS that don't already exist in your database" ...
"and it will only ever create the tables that don't exist."

If the table already exists, you will need to manually modify it. For
sqlite3, I would suggest the Firefox sqllite manager plugin, which
makes editing the tables pretty easy.

Zeynel

unread,
Dec 11, 2009, 5:10:00 PM12/11/09
to Django users
On Dec 11, 2:42 pm, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:

> If the table already exists, you will need to manually modify it. For
> sqlite3, I would suggest the Firefox sqllite manager plugin, which
> makes editing the tables pretty easy.

You are right. I did upload the new models.py and run python2.5
manage.py synchdb and then ./reset but I am getting
TemplateSyntaxError: no such column: wkw1_lawyer.firm_url

so the new columns were not created. I will have to just start from
scratch and create a new project.

Nick Arnett

unread,
Dec 11, 2009, 5:23:29 PM12/11/09
to django...@googlegroups.com
On Fri, Dec 11, 2009 at 11:36 AM, Zeynel <azey...@gmail.com> wrote:


So, it seems that, according to documentation, syncdb will create the
new tables?

Yes, it will create *new* tables.  It will not alter existing tables.

If you don't need to save your existing data, the easiest thing is to drop the old database and re-create it, then let syncdb create all the tables again.

If you do need to save old data, you could either add the columns yourself or rename the existing table, use syncdb to create the new version, then do something like INSERT INTO foo (id, bar, bat) SELECT id, bar, bat FROM old_foo.

Nick

Shawn Milochik

unread,
Dec 11, 2009, 7:50:48 PM12/11/09
to django...@googlegroups.com
I feel like a broken record this week -- I've been recommending South in every other post I've made on this list.

So, at risk of annoying people, check out South: http://south.aeracode.org

It does exactly what you want it to do, and is the dominant (and nearly the only) solution used in the Django community.

Shawn


Phlip

unread,
Dec 11, 2009, 10:43:18 PM12/11/09
to Django users
> syncdb will not alter an existing database.

Maybe the Django authors are obeying the DB ideal that if you could do
without a field for the first few records, then that field, when it
arrives, should be normalized out into its own table.

(Roughly speaking, a field with too many NULL or False entries is a
candidate to be a child record.)

Just recently I added to a SKU record a new boolean - MembersOnly -
and I added it by creating a new table, whose only record is a sku_id.
I did this because I didn't want to ask my colleagues if they wanted
to run syncdb on a live database, and it seems I guessed right!

But this sophistry won't help anyone when they have to run ALTER
TABLE... (-:

--
Phlip

Zeynel

unread,
Dec 12, 2009, 11:36:28 AM12/12/09
to Django users
On Dec 11, 5:23 pm, Nick Arnett <nick.arn...@gmail.com> wrote:

> If you don't need to save your existing data, the easiest thing is to drop
> the old database and re-create it, then let syncdb create all the tables
> again.

Yes, I deleted the content of the sqlite3 database file and then ran
syncdb and it created the new database. Thanks.

Zeynel

unread,
Dec 12, 2009, 11:40:09 AM12/12/09
to Django users
Shawn, Since I did not want to save the data I deleted the db file and
ran synchdb and that created a new database. But eventually I will use
south because I will keep adding new columns to this database. Thanks.
Reply all
Reply to author
Forward
0 new messages