Archive for the ‘Programming’ Category

ValueError in Django migration

Sunday, May 15th, 2016

I’ve recently started developing in Python+Django again for a personal site that I’m working on and for far too long today I’ve been pulling my hair out when trying to write what should be a fairly simple migration using migrations.RunPython() to generate a default value to assign to a new OneToOneField column.

The rather confusing error message that I was receiving is:

ValueError: Cannot assign "<User: blah>": "Profile.account" must be a "User" instance.

This seems to be the same problem described at, but nothing mentioned in that post worked.

I was hopeful when I came across that this was a known issue, but alas whilst similar it’s not exactly the same problem and that bug was closed over a year ago. Back to square one!

What was particularly confusing was that the same code seemed to work as expected when run in the Django shell:

from foo.models import Profile, Article
from django.contrib.auth.models import User
first_superuser = User.objects.all().filter(is_superuser=True).first()
default_profile = Profile(account=first_superuser, biography="")

After a lot of fiddling, I eventually realised that the way which I was importing the User model from the built in Django auth system was wrong for a migration and so I replaced:

from django.contrib.auth.models import User


User = apps.get_model('auth', 'user')

And now the migration finally works as expected. 🙂

ImageMagick and PHP 5.4

Friday, September 13th, 2013

When building the PHP imagick module for ImageMagick on a server running PHP 5.4 (a cPanel/WHM box in this case), you receive the following error:

/root/tmp/pear/imagick/imagick_class.c: In function ‘zim_imagick_setfont’:
/root/tmp/pear/imagick/imagick_class.c:1442: error: ‘struct _php_core_globals’ has no member named ‘safe_mode’
/root/tmp/pear/imagick/imagick_class.c:1442: error: ‘CHECKUID_CHECK_FILE_AND_DIR’ undeclared (first use in this function)
/root/tmp/pear/imagick/imagick_class.c:1442: error: (Each undeclared identifier is reported only once
/root/tmp/pear/imagick/imagick_class.c:1442: error: for each function it appears in.)
/root/tmp/pear/imagick/imagick_class.c:1442: error: ‘CHECKUID_NO_ERRORS’ undeclared (first use in this function)
/root/tmp/pear/imagick/imagick_class.c: In function ‘zim_imagick_setimageprogressmonitor’:
/root/tmp/pear/imagick/imagick_class.c:9534: error: ‘struct _php_core_globals’ has no member named ‘safe_mode’
/root/tmp/pear/imagick/imagick_class.c:9534: error: ‘CHECKUID_CHECK_FILE_AND_DIR’ undeclared (first use in this function)
/root/tmp/pear/imagick/imagick_class.c:9534: error: ‘CHECKUID_NO_ERRORS’ undeclared (first use in this function)
make: *** [imagick_class.lo] Error 1
ERROR: `make’ failed

The solution is to install the beta version of the module instead:

pear config-set preferred_state beta
pecl install imagick

WHMCS v4.41 requires BCMath support in PHP

Saturday, December 18th, 2010

Beware if you are upgrading to the newly released WHMCS v4.41 that some bits of the admin area such as the client profile tab seem to require that your web server’s copy of PHP has BCMath support available and enabled.

Look for “BCMath support => enabled” in your phpinfo() output from “php -i” to make sure that you have the BCMath library compiled in to your PHP or available as a shared library to be dynamically loaded at runtime as an extension.

Remember that on some web servers the PHP binary that you use from the command line isn’t always using the same configuration as the mod_php or PHP CGI used by your web-server, so you might get different results from “php -i” and calling phpinfo() in a web page.

For some reason WHMCS didn’t feel the need to mention this new requirement in the release notes for version 4.41.