model django new tables - django

I just created all the DB tables from one model with 6 classes.
In one of them I would like to add a NEW field.
I did that and hit: python manage.py syncdb.
But Django won't add that column in that table.
Am I missing something?
How can i add columns/tables AFTER I already created the db in Django.
Thanks!

You need to use a migration tool such as South.

Chapter 10 of the Django book has a section called 'Making Changes to a Database Schema' with a sub-section titled 'Adding Fields'...

Related

A field in my Django model already exists in the PostgreSQL database

I have a Django app with a model MyModel and some field my_field. My PostgreSQL database already has a field of that name under the mymodel table, since it was added manually in the past. When I make migrations, Django generates an AddField operation, which is in conflict. Currently I manually edit every generated migration to remove the conflict.
Is there a way to tell Django that the field already exists?
Unfortunately this is a live product, so I cannot lose any data.
Generate the migration that adds the field and then run python manage.py migrate <app_name> <migration_name> --fake to mark the migration as applied without actually running it
Thank you Iain Shelvington.

Can I add any column after I created My database?

I created my database using python manage.py syncdb And I tried to add another attribute to my model called created_date My site gives error. And I deleted my db.sqlite3 file Then reorganize my model Then error went. I want to know is this correct
As the comment says migrations are the way to do this. Native migrations were only introduced in Django 1.7 but, since you're using syncdb, I'm guessing you're using an earlier version.
For earlier versions of Django you need a third-party app called South to handle migrations for you. This will then let you change your database after creation fairly painlessly in most cases.

how to sync django models with pre-existing database?

I am having a hard time trying to come up with a reasonable design for my project. I have an existing Postgres database that gets constantly updated from other Python scripts.
The web server built on Django framework will access the Postgres database to update User models only and display blog information for the logged in Users. The blog information is what is being updated overnight by other Python scripts.
Now my question, if I have to syncdb my blog model with existing Postgres database, would that cause any problem?
ex:
models.py
class Blog:
title=...
content=...
author=....
And say my Postgres db called mydb has many tables, one of which is blog table and contains columns for title, content and author.
How would make my model in sync with existing database?
Now lets say I included a new column in my db which is date of entry.
If I simply update my model to :
class Blog:
title=...
content=...
author=....
date of entry=...
will it work.
what are the potential problems here and any simpler solutions for these?
P.S: I have used South in the past. but the situation here is different. I am using db that is read-only from Django's point of view, and no data migration is necessary as of now.
If your database is read-only, you don't have to do syncdb. Use managed=False and the db_table meta option on your model to specify the table name it corresponds to, and likewise for the field column names.
If you haven't already, see the doc on legacy databases for more info.

When you change the fields in the models in django

After you initially create the model and you want to change the fields in the models. If you syncdb ,it replies no fixtures found, meaning to say the models has not recognised the change. One way to solve this is to delete the database and recreate the database but you lose all the data. Is there any other better solution? Thanks...
Use south to help you for models changes. It will automatically detect any changes to models and generate appropriate script to update db schema.

Extending South Introspection in Django

I have a custom app which I wanted to start using South with. It uses a FK for associating a blog entry from another app (proprietary, not written by me). Problem is when I try to run the initial schemamigration I get an error that some of the blog entry app's fields cannot be frozen. The fields that can't be frozen are fields that uses custom fields extended off of the core fields.
./manage.py schemamigration free --initial
I read South's docs on extending introspection but I don't know where/how to define the introspection rules.
Thanks in advance for any suggestions.
Have you tried the South Field Triple approach instead?