views:

103

answers:

4

I'm new to Django (and databases in general), and I'm not sure how to structure the following. The sources of data I'll have for my site are:

  1. a blog
  2. for a few different games:
    • a high score list
    • user-created levels

If I were storing the data in ordinary files, I'd just have one file for each of the above. In Django, ideally (I think) I'd have a separate database for each of these, but apparently multiple database support isn't there for Django yet. I'm worried (unnecessarily?) about keeping everything in one database for two reasons:

  1. If I screw something up in one of the sections, I don't want to mess up the rest of the data.

  2. When I'm working on one of these sections, I'd like the freedom to easily change the model around. Since I've learned that syncdb doesn't, in fact, sync the database, I've decided that the easiest thing to do when messing around with a model is to simply wipe the database and start over. Again, I'm worried about messing up the other sections. I looked at south, and it seems like more trouble than it's worth during the planning stages of an app (but I'll reconsider later when there's actually valuable data).

Part of the problem is that I'm not really comfortable keeping my data in a binary format. I'm used to text, so I can easily diff it, modify it in an editor, etc., without going through some magical database interface (I'm using postgresql, by the way).

Are my fears unfounded? How do people normally handle this problem?

+1  A: 

Syncdb should really only be used for development. That's why it doesn't really matter if you wipe the tables and start again, perhaps exporting look up data into a json file that you can import each time you sync.

When your site reaches production however, you have a little more work. Any changes you make to your models that need to be reflected in the database, need to be emitted as SQL and run manually on the database. There's a django-admin.py function to emit the suggested SQL, which you can use to build up a script to run on the database to migrate it. Like you mention, a migrations app like South can be beneficial here but it's not strictly needed.

As far as your separation of sites goes, run them as separate sites/projects. You can have a separate settings file per project which allows you to run two different databases. This is in contrast to running the two sites as separate apps within the same project. If they're totally separate they probably shouldn't be in the same project unless you need to leverage common code.

Josh Smeaton
If there is a significant quantity of common code, it can always be factored out into a module / set of modules to be imported by all relevant Django projects. Just make sure to arrange things so that the shared code module is on the Python path from the point of view of your Django code.
Michał Marczyk
The problem with running as different projects is that they're going to under the same website "umbrella", so their base html templates should be the same. Also, there are going to be links to/from them, for which Django provides a nice way to avoid repetition with reversing the url pattern matching..
Jesse Beder
@Michał Marczyk, sorry, I didn't see your comment until after I posted, but your solution (I think) only works for common logic, not common templates (which really belong within one project)
Jesse Beder
Why do say syncdb should only be used for development? In my experience that isn't the case - how else would you prepare a new database to be used by Django?
John Debs
John, if you're adding new tables then syncdb is fine to use in production. I wasn't considering that case.
Josh Smeaton
+2  A: 

In short, your fears are unfounded. You should "organize" your database by project to use the Django term. Each model in each app will have it's own table, but they will all be in the same database. Putting them in a separate database isn't a good idea for a whole host of reasons, the biggest is that you cannot query across databases.

While I agree that south is probably a bit heavy for your initial design/dev stages it should be considered seriously for anything even resembling a beta version and absolutely necessary in production.

If you're going to be messing with your models a bunch during development the best thing to do is use fixtures to load data in quickly after running sync. Or, if you are going to be changing a bunch of required fields, then write some quick Python to create dummy data for you.

As for not trusting your data to binary, a simple "pg_dump " will get you a textual version of your data. It sounds to me like you're working on your app against live production data, which is a mistake. Your goal should be to get your application built, working, and tested on fake data or at least a copy of your production data and then when you're sure everything is golden migrate it into production. This is where things like south come in handy as you can automate this deployment and it will help you handle any database table/column changes you need to make.

I'm sure all of this sounds like a pain, but all of it is able to be automated and trust me it makes your life down the road much much easier.

Frank Wiles
Thanks for the explanation. I'm still looking around, but this seems to be the consensus. Incidentally, I'm not working against live production data, but it seems that I'll have to do some things against live data (like updating tables, etc.), which is still a bit opaque.
Jesse Beder
+4  A: 

I generally just reset the module

>>> python manage.py reset blog

this will reset all tables in INSTALLED_APPS.blog

I'm not sure if this answers your question but it's much lest destructive than wiping the DB.

czarchaic
+3  A: 

For what it's worth, I totally understand your frustration as I went through a really similar thought process when starting. Unfortunately, there isn't much you can do (easily, anyway) besides get familiar with the tools you'll be using.

First of all, you don't need multiple databases for what you're doing - one will do. Each app will create its own tables in the database which are somewhat isolated from one another. As czarchaic mentioned, you can do python manage.py reset app_name to reset just one of them in case you change your model. You will lose that data, though.

To get data in relatively easy to work with format, you can use the command python manage.py dumpdata > file_name.json, and then to reload it later python manage.py loaddata file_name.json. You can use this for backups, local test data, or as a poor man's migration (hint: South would be easier).

Yet another option is to use a NoSQL database for any data you think will need extra flexibility. Just keep in mind that Django doesn't have support for any of these at the moment. That means no no admin support or ModelForms. Of course, having a model may become unnecessary.

John Debs