July 2011
1 post
django manage.py loaddata without constraints
I had to quickly inject a dumpdata json file on a mysql server and got this nice message:
IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails
I created a settings file for the loaddata command without mysql foreignkey checks.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
"init_command":...
March 2010
1 post
6 tags
Django thumbnails, resize image with PIL and...
There is a wide variety of django filters and tags to crop, resize or make a thumbnail out of an image but most of them seem to reinvent the wheel.
How to resize, crop or create a thumbnail with python and PIL
You need to install the Python Image Library and this snake image to run this example.
#!/usr/bin/env python
try:
from PIL import Image, ImageOps
except ImportError:
import...
February 2010
2 posts
3 tags
GeoDjango with Django 1.2
I’ve been struggling with trying to use geodjango with django 1.2 when all you need to do is update your database settings.
If you are encountering errors like:
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'
AttributeError: 'module' object has no attribute 'GeoSQLCompiler'
Set your DATABASE_ENGINE correctly:
'ENGINE':...
4 tags
Looking for a domain name suggestion?
Computer suggestions
I use Visual thesorus to find interesting keywords and mix them with bustaname.com to find available suggestions.
Human suggestions
I am trying Picky domains and will update this post with results.
I have made the $50 payment and am waiting for further instructions.
January 2010
5 posts
3 tags
SQL update for your django models, old school...
Let’s say you have added a new field to the profile model.
Use sqlall <app> to generate the SQL.
./manage.py sqlall profile
BEGIN;
CREATE TABLE `profile` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`user_id` integer NOT NULL UNIQUE,
`new_field` integer UNSIGNED NOT NULL
)
;
ALTER TABLE `profile` ADD CONSTRAINT `user_id_refs_id_59efa1d8` FOREIGN KEY (`user_id`)...
2 tags
Always import your django settings this way!
from django.conf import settings
If you don’t, the settings won’t contain the default values.
Django settings documentation
Here’s the algorithm Django uses in compiling settings:
Load settings from global_settings.py.
Load settings from the specified settings file, overriding the global settings as necessary.
Links
...
2 tags
Install django-extensions!!!
pip install -e git+git://github.com/django-extensions/django-extensions.git#egg=django_extensions
Automatically import your applications models in your python shell
./manage.py shell_plus
From 'auth' autoload: Permission, Group, User, Message
From 'my_app' autoload: MyModel
>>>
Visualize your project
./manage.py graph_models -a -g -o...
2 tags
Set MySQL storage engine to InnoDB for Django...
The advantage of InnoDB tables is transactions.
If you make multiple edits to the database and something goes wrong, you won’t be left with half the data updated. Either everything gets written or nothing.
Open your settings file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '',
'USER': '',
...
1 tag
Upload a static file on tumblr →