Do I have to drop tables once i change the django models - django

I am now to Django and i find that every time i change my models.py, i have to drop some tables to get things working again.Is there any better way?I really hate to drop tables.
Also,is it a problem that i find myself change models.py?what you would do if your site is up and running when you find you need to add something else to the models?

have a look at south

Related

Migrations on Through Model Django Many to Manyo Field

I have a model used in a ManyToManyField that I want to change.
User
Teacher
- ManytoManyField through TeacherRelation
TeacherRelation
NewModel
I want to change the foreign key of the TeacherRelation so it uses NewModel instead of Teacher . I already have data That I dont want to loose on the db, so I would like to know what's the best way to make this kind of migrations. Im concerned about issues like:
Will I have to delete the previous entries on the db that used the other foreign key?
Is there a way to find all the previous entries and replace them with the correct new primary key? I think I'll have to write a script for that... But where and when should I place it and run it?
Thanks for the help.
you can manually duplicate the table. If you are using postgres, you can use PGadmin III or the console. for sqlite you ca send basic SQL commands to duplicate Teacher.
After Duplicating teacher, just change the original teacher table name to "NewModel" and build it as you wish :)
Easiest option IMO.

Does Django syncdb sync edited models?

I have come across an interesting problem. I first created a model. And syncdb'ed it. Later on I changed the model by adding one models.DateField() and a models.DateTime(). Than I re syncdb'ed it... However the when I checked the database the rows were not added. Is this normal behaviour or did I do something wrong? Later on I deleted all tables and syncdb'ed and got all rows with no problems.
No, the docs are quite explicit that it does not alter existing tables.
Use something like South.
Django itself doesn't support db migrations. You will need to look into South for this.
This is a good place to start.

Single model for storing django application-wide options

I have a set of top-level configuration data fields that I want to be able to set within django admin for each deployment of my django app. I only want one set of these.
Example fields: site_logo, contact_person, address, facebook_url, twitter_url
The problem is that Django Admin is geared towards tables (lists) of models, so its not a good fit for this type of singular configuration model. I really only want one of these models to exist for the whole site, and to be able to click into it from admin and edit the various fields.
It seems i've come across a 3rd party app in the past to accomplish this but can't find it anywhere. Part of the problem is I'm finding it difficult to find the right words to google. Any ideas?
It looks like django-values will do what you're looking for.
Other possible contenders:
http://github.com/sciyoshi/django-dbsettings (doesn't look maintained)
http://github.com/jqb/django-settings
Have a look at django-livesettings it sounds like it might fit.
Not that i have used it, but i have heard good things about django-constance.
And there are even some more options listed in the Configuration-Grid on Django Packages.

Django -- add model to database without losing data

I have a simple Django website (just a form, really) which asks a few questions and saves the data in a SQL database using Model.save(). Pretty simple. I want to add a model to do page counting, though -- it'll just be a single object with a field that gets incremented each time the page's view function is called.
Now, I know little to nothing about SQL. I imagine this is not terribly difficult to do, but I would like to avoid losing or breaking all my data because of a slight misunderstanding of how the database works. So how can I go about doing this? I've heard of some third-party apps that will implement such functionality, but I'd like to do it myself just for learning purposes.
I don't understand why your existing data would be affected at all. You're talking about adding a completely new table to the database, which is supported within Django by simply running manage.py syncdb. The case where that doesn't work is when you're modifying existing tables, but you're not doing that here.
I must say though that learning and using South would be of benefit in any case. It's good practice to have a tool that can maintain your model tables.
(Plus, of course, you would never lose any data, because your database is backed up, right? Right?)
Since you're adding new model, you can just run syncdb and it will create new table for your model. If you were to change existing model, then you'd need to manually update database schema using "ALTER TABLE" statements or use South instead.

How do I make changes to a model in Django?

How does Django handle changes to my Model? Or, what help does it offer me to do this?
I am thinking of a situation where I have already have published data to my DB which I don't want to lose, but I need to make changes to my data model - for example, adding extra fields to a particular class, changing the types of fields, etc. My understanding is that syncdb won't ever alter tables that already exist in the DB.
For example, let's say I have the following model:
class Person(models.Model):
name = models.CharField(max_length=200)
phone_number=models.CharField(max_length=200)
hair_colour=CharField(max_length=50)
Things I might want to do to Person off the top of my head:
I wish to add an 'age' field.
I realise I want to use IntegerField instead of CharField for phone_number (whether this is a good idea or not, is out of scope...) - assuming it's possible.
I realise that I no longer wish to define hair_colour 'inline' within Person, because several people share the same hair colour - I wish instead to change this to be a foreign key to some other model.
Whilst I can imagine some of these are tough/impossible for the framework to 'guess' exactly what needs to be done to my data if all I do is update the models.py, I can imagine that there might still be some tooling to help enable it - does it exist?
In particular I imagine there must be some good patterns for option 1.
I'm very new to Django and have no experience with any other ORM-type stuff, which I think this is - I've always been a bit suspicious of ORMs, mainly for the reasons above :)
Django itself will not attempt to modify an already created database table. What you are trying to do is typically called "Migration" and there are a couple of different Database Migration tools available for Django.
South
Schema Migrations
Data Migrations
Backwards Migrations
Nash Vegas
Schema Migrations
Data Migrations
Django Evolution
Schema Migrations
Data Migrations (Unknown)
Backwards Migrations (Unknown)
Of the three South is probably the most widely used but they each have different ways of dealing with migrations. You can see more details on the comparison on Django Packages.
Much of what you're asking about can be done with the django project South. You add it as an INSTALLED_APP. Create a baseline, then as your model changes it creates SQL statements to convert your tables and the rows with-in the tables to the new model format.