tech notes

These posts are strictly for programmers to find on google when trying to solve the same problems. Otherwise dull as dirt.

django.db.utils.DatabaseError: relation "django_content_type" does not exist

2011 January 19






I was getting an error while running unit tests and the test database was failing to be created.

django.db.utils.DatabaseError: relation "django_content_type" does not
exist

eventually found the problem.  I had the following in a file that was imported to a models.py:


hrd_type = ContentType.objects.get_for_model(HttpReferrerDomain)


The idea being that it can just set the var when it loads up and after that its always there.  But this means for creating a fresh database, the db is called to do this lookup right when the models.py is loaded.

more...


django has two classes called ValidationError

2010 February 04

There is one in django.core.exceptions and one in django.forms.util


Using that space age IDE Eclipse I have to say I’m enjoying how much time I’ve saved just going shift-command-O to organize and resolve all of my imports.  But today I’ve just lost a few hours due to my ok-ing the wrong class.


Quite mysterious it was, I raised a ValidationError (core exceptions one) in my form’s clean() and watched as the try: except ValidationError: in django’s full_clean() completely ignored my error and propagated it up.


But what is the django crüe doing naming two classes that ?  django.core.exceptions is quite vague, it just says “An error while validating data.”  Its used for db Field validation errors.  


I guess its as confusing as naming two entire class heirarchies 'Field’ : one for db fields and one for html form fields.  As though the two always went hand in hand, which they don’t.  


Another large problem I have recently is that a form field and its form widget are never allowed to talk to the object model, only to a single db field. I often have built fields that need to save to several fields on the model, and the clearest place to do this is by concentrating most of the power into the Form class.  


An example is an address input that I use that has a display address, postal address and map address.  The interface is complex, uses google maps and in the end also saves longitude,latitude, geo_accuracy, address, street, zip, city,state, and country objects. As a field or widget its not allowed to do all that.  It has to be tied to a form or the form has to call a function on the field to offer it access to the Model.


Another pet peeve (while we’ve got me in a tizzy still):


 


    # Give this new form class a reasonable name.

    if form ModelForm:
        class_name = model.__name__ + 'Form'
    else:
        # CX: leave a clue that the form IS the form that you passed in
        # otherwise you have no clue what happened to your form class
        class_name = form.__name__ + "FromFactory"

 

    # Give this new form class a reasonable name.

    if form ModelForm:


        class_name = model.name + 'Form’


    else:


        # CX: leave a clue that the form IS the form that you passed in


        # otherwise you have no clue what happened to your form class


        class_name = form.name + “FromFactory”


In django’s normal trunk they take the ModelForm class that you wrote, they jack it into some cyborg reconstruction routine wherein it seems that your class is copied into another class and then …  given the same name as your class and even inserted into the module that you wrote yours in.  This makes debugging hell.  You think you are looking at your class, but its been factoried.  Hence my change above to the naming convention so that the poor programmer can be informed that something happened.


ok, back to unit testing hell.

 


 


 

more...


GDAL fails to build: `.rodata' can not be used when making a shared object; recompile with -fPIC

2010 January 23

 


libtool: link: g++ -shared -nostdlib /usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.1/crtbeginS.o .libs/libgdal.la.lnkscript  -L/usr/local/lib /usr/local/lib/libgeos_c.so /usr/local/lib/libgeos.so /usr/local/lib/libexpat.so -L/usr/lib -lpq -lrt -ldl /usr/lib/libcurl.so -lssl -lcrypto -lz -L/usr/lib/gcc/x86_64-linux-gnu/4.4.1 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../.. -lstdc++ -lm -lc -lgcc_s /usr/lib/gcc/x86_64-linux-gnu/4.4.1/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crtn.o         -Wl,-soname -Wl,libgdal.so.1 -o .libs/libgdal.so.1.13.2

/usr/bin/ld: /usr/local/lib/libz.a(crc32.o): relocation R_X86_64_32 against `.rodata’ can not be used when making a shared object; recompile with -fPIC

/usr/local/lib/libz.a: could not read symbols: Bad value

collect2: ld returned 1 exit status

make1: *** [libgdal.la] Error 1

make1: Leaving directory `/home/crucial/tmp/gdal-1.6.2’

make: *** [check-dlib] Error 2

Compiling GDAL, I had a failure with the following splurge:


libtool: link: g++ -shared -nostdlib /usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.4.1/crtbeginS.o .libs/libgdal.la.lnkscript  -L/usr/local/lib /usr/local/lib/libgeos_c.so /usr/local/lib/libgeos.so /usr/local/lib/libexpat.so -L/usr/lib -lpq -lrt -ldl /usr/lib/libcurl.so -lssl -lcrypto -lz -L/usr/lib/gcc/x86_64-linux-gnu/4.4.1 -L/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../.. -lstdc++ -lm -lc -lgcc_s /usr/lib/gcc/x86_64-linux-gnu/4.4.1/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crtn.o         -Wl,-soname -Wl,libgdal.so.1 -o .libs/libgdal.so.1.13.2


/usr/bin/ld: /usr/local/lib/libz.a(crc32.o): relocation R_X86_64_32 against `.rodata’ can not be used when making a shared object; recompile with -fPIC


/usr/local/lib/libz.a: could not read symbols: Bad value


collect2: ld returned 1 exit status


make1: *** [libgdal.la] Error 1


make1: Leaving directory `/home/crucial/tmp/gdal-1.6.2’


make: *** [check-lib] Error 2


This is the linker (ld) failing to link libz which is the zip library.  That library on my ubuntu was not compiled in such a way as to allow linking as a shared object.

The solution is to download the zlib source from http://www.zlib.net/

# and configure it to be shared, linkable as a shared library

./configure —shared

make

# but don’t do this yet

# sudo make install

GDAL will now compile, but then I find that libxml2 is unhappy with the zlib I’ve just compiled.  


the zlib.org source code compiles and then installs it here:

ls -l /usr/local/lib/

BUT this version does not have `gzopen64`.  Ubuntu is, I guess, using a different version that does include that, and libxml2 uses that, php5 uses libxml2, and apache has php5 in the http.conf so the server will gracefully decline to restart.  and thank heavens for that gracefulnes.

so we have the ubuntu libz still here, with the `gzopen64`:

ls -l /usr/lib/libz*

-rw-r—r— 1 root root 118210 Sep  8 06:02 /usr/lib/libz.a

lrwxrwxrwx 1 root root     20 Nov 15 13:51 /usr/lib/libz.so -> /lib/libz.so.1.2.3.3

and the zlib.org one is over here:

ls -l /usr/local/lib/

[it was here, really]

from within the zlib-1.2.3 source folder uninstall that one:

sudo make uninstall

We are going to keep this zlib in a local personal folder, not in the main paths and gdal will be compiled linking to this zlib.

move this zlib folder into my personal ~/bin/

mv zlib-1.2.3 ~/bin/

go back to the gdal dir

./configure —help

shows this option:

  —with-libz=ARG       Include libz support (ARG=internal or libz directory)

# with which we will feed it the (—shared enabled) zlib

# note: it doesn’t like relative or personal paths

./configure —with-libz=/home/crucial/bin/zlib-1.2.3/

make

sudo make install

GDAL compiled,

apache restarted without complaint


and I have another new and exciting bug waiting for me.

btw. I’m really sick of TinyMCE screwing up my posts.  Time to get rid of it.

 


 

more...


installing MySQLdb on Ubuntu (mysql-python)

2010 January 23

MySQLdb is the python support bindings for MySQL.  Not that the name would lead you to beleive that.
Its sourceforge page calls it http://sourceforge.net/projects/mysql-python/ which makes more sense.
you need setuptools, which you usually already have:
    sudo aptitude install python-setuptools
You need MySQL-devel to compile, but its not called that, its called: libmysql++-dev on Ubuntu
    sudo apt-get install libmysql++-dev
download MySQLdb itself from:
    http://sourceforge.net/projects/mysql-python/
    # the version you download will be more recent
    tar xfz MySQL-python-1.2.3.tar.gz
    cd MySQL-python-1.2.3
    python setup.py build
    # the following might fail if it can’t find mysql_config on your path
    sudo python setup.py install
    
if it did try locating the mysql_config that was installed with MySQL-devel
and editing site.cfg which is in the MySQL-python folder
uncomment and point to it here: 
    mysql_config = /usr/bin/mysql_config
  
and try to build again
if it worked then test:
open a python shell (also change directories out of the compile dir you were just in to avoid confusing yourself with the just compiled version sitting in your path)
    import MySQLdb

MySQLdb is the python support bindings for MySQL.  Not that the name would lead you to beleive that.


Its sourceforge page calls it http://sourceforge.net/projects/mysql-python/ which makes more sense.


you need setuptools, which you usually already have:


    sudo aptitude install python-setuptools


You need MySQL-devel to compile, but its not called that, its called: libmysql++-dev on Ubuntu


    sudo apt-get install libmysql++-dev


download MySQLdb itself from:


    http://sourceforge.net/projects/mysql-python/


    # the version you download will be more recent


    tar xfz MySQL-python-1.2.3.tar.gz


    cd MySQL-python-1.2.3


    python setup.py build


    # the following might fail if it can’t find mysql_config on your path


    sudo python setup.py install


if it did try locating the mysql_config that was installed with MySQL-devel


and editing site.cfg which is in the MySQL-python folder


uncomment and point to it here: 


    mysql_config = /usr/bin/mysql_config


and try to build again


 


if it worked then test:


open a python shell (also change directories out of the compile dir you were just in to avoid confusing yourself with the just compiled version sitting in your path)


    import MySQLdb


 


 

more...


postgres login as admin user postgres

2010 January 18

When installing postgres a user will be created named 'postgres’ with a password of '!!’ which means “cannot login”.  But yet you need to login as that in order to run psql (the postgres db shell) to create other users and to create database templates.


The solution is to first log yourself in as root (in your normal shell):


su root


(enter password…)


then you will no longer be subject to password checks and you can login in as user 'postgres’


su postgres


(you will automatically allowed to do this)


then you can


psql


 


note: on OS X you cannot login as root.  use a binary install.  the King Khaos package was fairly painless for me.

more...


Backwardation : Version 12"
coltan and cassiterite Timeblind : Solar Life Raft Ingredients