Showing posts from August, 2011

Django: Virtualenv with Eclipse befrending

Many people use Virtualenv. It has become common pattern now days. I like Virtualenv but it has no debugger. You can run 'python runserver' but it will newer help you see somthing like runtime variables and code flow. I like PyDev debugger. It helps me a lot.
Anyway I faced a problem recently. My employer wants me to run code at virtualenv. I needed to download and run project. I made it creating New virtualenv called 'venv' inside a project folder. Command is:

virtualenv --no-site-packages venv

It will create dir 'venv' inside current directory. So you should be at where you need to store your virtualenv (your project directory for e.g.). --no-site-packeges parameter says that your new virtual environment will not have packages installed in system. It's a must if you want to avoid collisions with newer versions of code.

Now we have a dir inside your project called venv containing our separated python environment. Lets activate it using command…

Django: How and why to use migrations. Django-South.

Hi there guys and we're here to talk about migrations today. My app grown to complex app with profiles, social registration permits and so on. First time I've decided to make myself a simple app. Now it became complex enough and contains enough code to need code comments :). Anyway Migrations is a process anybody someday will need. I thought it's hard to learn or understand, but it's not.

Main point is that you:
- save your current database tables structure
- change your model tables
- scan for differences and create script
- then write changes to your database with automatically generated python script.

Sound's simple? I't not all so simple in fact. App that you need to learn is Django-south. Its main objectives are to provide a simple, stable and database-independent migration layer to prevent all the hassle schema changes over time bring to your Django applications. It has quite understandable tutorial here.

So if you're tired of ALTER'ing your table…

Django: adding code execution on your app syncdb, or how to use Django Signals.

Hi there and let's talk about app initialization in Django. There are some cases when you want to initialize a Django app with creating some default values in database. In my case it was necessity to create default album in database to post user photos to. Sometimes you could just use get_or_create for those purposes. But it will be a good example if we will need something more complex in our app initialization; for e.g. generating thumbnails for photos or cleaning unused temporary files etc. So let's get started:

Good place to put your initialization scripts is your app's file. You can examine Djangoproject wiki for more info. Anyway here is my code for making this:

from django.db.models.signalsimport post_syncdb import models from models import Album def create_first_album(sender, **kwargs): """    Create your album sequence to create default album to post photos to    checks for existence of this album and creates one if none exists.    ""&q…