Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Archives for django

ForeignKey to custom user model in Django

0 Comments

Came across this problem when running a syncdb in Django (>=1.5) :

CommandError: One or more models did not validate:
APP.ModelName: 'user' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL.

So you define a custom user model and add the appropriate line to the settings.py file. Then you add a ForeignKey from a model in your app the way the Django doc tells you to. And then you see this (the one above) error when you run a syncdb.

We hit upon this problem. The reason this happened was that the AUTH_USER_MODEL setting was below the INSTALLED_APPS in the settings.py file. Django will not handle forward referencing in this case. The solution is to simply move the AUTH_USER_MODEL setting above the INSTALLED_APPS settting.

Filed under django
Jul 16, 2013

Django 1.4 on CentOS 5.8

0 Comments

The default python version (2.4.3) on Centos 5.8 is not supported for Django 1.4 . Here's how to get Django 1.4 running on Centos 5.8 . Steps here are probably useful for future/different versions.

Step 1 : Enable EPEL

EPEL has newer version of Python available in it's repo. The first step is to enable it. The command you need to enable the EPEL repo is :
# rpm -ivh http://mirror.chpc.utah.edu/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
Once you install the package you've enabled EPEL.

Step 2 : Install Python 2.6 and easy_install

#yum install python26-distribute
This will install python 2.6 and easy_install

Step 3 : Install pip

#easy_install-2.6 pip
Now that you've pip installed, we can get started on install virtualenv and virtualenvwrapper.

Step 4 : virtualenv and virtualenvwrapper

#pip install virtualenvwrapper
will install virtualenvwrapper and virtualenv. Now you need to add some lines to your .bashrc
export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python26
source /usr/bin/virtualenvwrapper.sh

(Make sure to create the ~/.virtualenvs directory if it isn't already created.)
Now activate the changes to your bashrc by doing
#source ~/.bashrc

Step 5 : Create a virtual environment for your Django project

You can do this by running
# mkvirtualenv my-env
Once you run this you will automatically switch to the new virtualenv. Your shell prompt will be prefixed with (my-venv)

Step 6 : Install Django

Change to the directory you want to install Django in. Then run :
(my-venv)#pip install Django

Bonus!

1. Installing MySQL-python :

First install
#yum install mysql-devel
and then install MySQL-python (my-venv)#pip install MySQL-python

2. Installing Gunicorn

(my-venv)#pip install gunicorn

3. requirements.txt

It's often easier to just have a list of pip packages to install for a particular project in a file. Conventionally, it's named requirements.txt
(my-venv)#pip install -r requirements.txt
will install all the packages given in the requirements.txt
(my-venv)#pip freeze
Will show you a list of packages installed by pip currently. Something like this :
Django==1.4
MySQL-python==1.2.3
wsgiref==0.1.2

You can take this list directly and put it into the requirements.txt
(my-env)# pip freeze > requirements.txt

Filed under django, Linux
Aug 24, 2012