Add field in models django 0.96

8 views
Skip to first unread message

Xan

unread,
Sep 25, 2007, 2:41:02 PM9/25/07
to Django users
Hi,

I use django 0.96. I have a simple model and I just want to add one
field to my existing model. How can I do that? I think that I can't do
that without SQL code. But unfortunely I don't know it.

Anyone could give me a example of SQL code for adding one field to one
model?

If I only have the solution to upgrade to django-cvs, how can I pass
all my data from django 0.96 models to svn django models?

Thanks in advance,
Xan.

Richard Dahl

unread,
Sep 25, 2007, 2:56:35 PM9/25/07
to django...@googlegroups.com
Xan,
as far as the sql code, it depends on what backend db you are using. 
Perhaps I am behind the times, but what differences are there in the 0.96 models to the latest svn trunk?  I am currently developing an app that I began pre - 96 and do not recall having to modify the models as I updated, I stay pretty current with trunk.  As far as getting the data out of the database, you can using manage.py to dump the tables into a fixture, or you can generally dump the data out of your database directly as a sql backup file.  That is what I have done when I have had to make significant changes to my models.  After I dump the data I manually edit it (lots of find/replace) and import it as a .sql batch file via postgres.  Not advocating this way as a best practice, but it worked for me.
-richard

Joe

unread,
Sep 25, 2007, 6:27:11 PM9/25/07
to Django users
The easiest way to to this is look at the create table SQL generated
by the manager.

python manage.py --settings=my_settings sql my_app

Then, run the appropriate query once you know the datatype created
when you added the column.

For example, in postgres:

ALTER TABLE myapp_mymodel ADD COLUMN mycol INT NOT NULL;

Xan

unread,
Sep 26, 2007, 10:52:45 AM9/26/07
to Django users
Thanks, what the equivalent in mysql?

Meanwhile, I think I will try patch to 0.96 found in
http://code.djangoproject.com/wiki/SchemaEvolutionDocumentation

Thanks another time,
Xan.

Derek Anderson

unread,
Sep 26, 2007, 12:12:44 PM9/26/07
to django...@googlegroups.com
if you want any help, feel free to pm me.

DJango Fett

unread,
Sep 26, 2007, 5:33:06 PM9/26/07
to Django users
Here are the steps I would use to add a field to an existing model
without having to write your own SQL and without the risk of getting
your model code out of sync with your database table. This code uses
the same objects in the Tutorial on http://www.djangoproject.com/documentation/0.96/tutorial01/.
Open up a Command Prompt window and change the drive/directory to your
project. Then do something similar to the following:

> python manage.py dumpdata polls > polls.json
> python manage.py sqlclear polls > polls_drop.sql
> mysql -u root -p pypoll < polls_drop.sql
enter your password for the root user (change for your specific
username)

edit mysite/polls/models.py and add the field to the Poll model

> python manage.py syncdb
> python manage.py loaddata polls.json

Hope this helps.

On Sep 25, 2:41 pm, Xan <xancor...@gmail.com> wrote:

Xan

unread,
Sep 27, 2007, 11:20:33 AM9/27/07
to Django users

On Sep 26, 11:33 pm, DJango Fett <tommy.adk...@gmail.com> wrote:
> Here are the steps I would use to add a field to an existing model
> without having to write your own SQL and without the risk of getting
> your model code out of sync with your database table. This code uses

> the same objects in the Tutorial onhttp://www.djangoproject.com/documentation/0.96/tutorial01/.


> Open up a Command Prompt window and change the drive/directory to your
> project. Then do something similar to the following:
>
> > python manage.py dumpdata polls > polls.json
> > python manage.py sqlclear polls > polls_drop.sql
> > mysql -u root -p pypoll < polls_drop.sql
>
> enter your password for the root user (change for your specific
> username)
>
> edit mysite/polls/models.py and add the field to the Poll model
>
> > python manage.py syncdb
> > python manage.py loaddata polls.json
>
> Hope this helps.

Okay, but I have 0.96 version (I have no loaddata option)
But, thanks very much,
Xan.

DJango Fett

unread,
Sep 28, 2007, 8:48:18 AM9/28/07
to Django users
I am using 0.96 version too. I tested the steps I provided before I
posted. Check out the 0.96 documentation yourself about the loaddata
option: http://www.djangoproject.com/documentation/0.96/django-admin/#loaddata-fixture-fixture

Xan

unread,
Sep 28, 2007, 9:32:25 AM9/28/07
to Django users
Sorry so,
I will try it.

Thank you very much for your support,
Xan.

On Sep 28, 2:48 pm, DJango Fett <tommy.adk...@gmail.com> wrote:
> I am using 0.96 version too. I tested the steps I provided before I
> posted. Check out the 0.96 documentation yourself about the loaddata

> option:http://www.djangoproject.com/documentation/0.96/django-admin/#loaddat...

Xan

unread,
Sep 29, 2007, 4:31:14 AM9/29/07
to Django users
Hi,

It worked but I HAVE A PROBLEM:
After done it, the database have strange caracters: it displays
"Convocatòria" instead of "Convocatòria"
I presume that it's an encoding problem. How can I solve that?

Please, anwer me
Xan.

On Sep 26, 11:33 pm, DJango Fett <tommy.adk...@gmail.com> wrote:

> Here are the steps I would use to add a field to an existing model
> without having to write your own SQL and without the risk of getting
> your model code out of sync with your database table. This code uses

> the same objects in the Tutorial onhttp://www.djangoproject.com/documentation/0.96/tutorial01/.

Reply all
Reply to author
Forward
0 new messages