<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>The pony is dead.</description><title>Djangosaur</title><generator>Tumblr (3.0; @djangosaur)</generator><link>http://djangosaur.tumblr.com/</link><item><title>django manage.py loaddata without constraints</title><description>&lt;p&gt;I had to quickly inject a dumpdata json file on a mysql server and got this nice message:&lt;/p&gt;

&lt;pre class="brush: python"&gt;
IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails
&lt;/pre&gt;

&lt;p&gt;I created a settings file for the loaddata command without mysql foreignkey checks.&lt;/p&gt;

&lt;pre class="brush: python"&gt;
DATABASES = { 
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
             "init_command": "SET foreign_key_checks = 0;",
        }        
    }       
}       
&lt;/pre&gt;

&lt;pre class="brush: bash"&gt;
export DJANGO_SETTINGS_MODULE="project.settings_without_fk_ckecks"
python manage.py loaddata data.json
&lt;/pre&gt;</description><link>http://djangosaur.tumblr.com/post/7842592399</link><guid>http://djangosaur.tumblr.com/post/7842592399</guid><pubDate>Wed, 20 Jul 2011 15:20:22 +0200</pubDate></item><item><title>Django thumbnails, resize image with PIL and python</title><description>&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;How to resize, crop or create a thumbnail with python and PIL&lt;/h2&gt;

&lt;p&gt;You need to install the &lt;a href="http://www.pythonware.com/products/pil/"&gt;Python Image Library&lt;/a&gt; and this &lt;a href="http://static.tumblr.com/pvhyhgn/3uUkyp9ky/snake.jpg"&gt;snake image&lt;/a&gt; to run this example.&lt;/p&gt;

&lt;pre class="brush: python"&gt;
#!/usr/bin/env python
try:
    from PIL import Image, ImageOps
except ImportError:
    import Image
    import ImageOps


image = Image.open('snake.jpg')

# ImageOps compatible mode
if image.mode not in ("L", "RGB"):
    image = image.convert("RGB")


imageresize = image.resize((200,200), Image.ANTIALIAS)
imageresize.save('resize_200_200_aa.jpg', 'JPEG', quality=75)

image.thumbnail((200,200), Image.ANTIALIAS)
image.save('thumbnail_200_200_aa.jpg', 'JPEG', quality=75)

imagefit = ImageOps.fit(image, (200, 200), Image.ANTIALIAS)
imagefit.save('fit_200_200_aa.jpg', 'JPEG', quality=75)
&lt;/pre&gt;

&lt;h3&gt;Image.resize&lt;/h3&gt;
&lt;img src="http://static.tumblr.com/pvhyhgn/3YJkyo13f/resize_200_200_aa.jpg" alt="image resize" style="float:right"/&gt;&lt;p&gt;&lt;strong&gt;resize&lt;/strong&gt; changes the aspect ratio of the image.&lt;/p&gt;

&lt;h3 style="clear: both"&gt;Image.thumbnail&lt;/h3&gt;
&lt;img src="http://static.tumblr.com/pvhyhgn/q6Jkyo141/thumbnail_200_200_aa.jpg" alt="image thumbnail " style="float:right"/&gt;&lt;p&gt;&lt;strong&gt;thumbnail&lt;/strong&gt; keeps the aspect ratio of the image and scales it to the defined area.&lt;/p&gt;

&lt;h3 style="clear: both"&gt;Imageops.fit&lt;/h3&gt;
&lt;img src="http://static.tumblr.com/pvhyhgn/5JLkyo14m/fit_200_200_aa.jpg" alt="image imageops fit" style="float:right"/&gt;&lt;p&gt;&lt;strong&gt;fit&lt;/strong&gt; keeps the aspect ratio and crops the image to the size of the defined area.&lt;/p&gt;

&lt;h3 style="clear: both"&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;We want our images to look nice so we will discard the resize function and implement &lt;strong&gt;thumbnail&lt;/strong&gt; and &lt;strong&gt;fit&lt;/strong&gt; inside django filters.&lt;/p&gt;

&lt;h2&gt;Django filters to scale and crop images&lt;/h2&gt;

&lt;p&gt;Our filter will receive an imagefield object (with path and url properties) and dimensions. It will check if the image has already been generated and serve it, otherwise, it will create a new resized image.&lt;/p&gt;


&lt;pre class="brush: python"&gt;
# my_apps/image/templatetags/image_tags.py
import os.path

from django import template

FMT = 'JPEG'
EXT = 'jpg'
QUAL = 75

register = template.Library()


def resized_path(path, size, method):
    "Returns the path for the resized image."

    dir, name = os.path.split(path)
    image_name, ext = name.rsplit('.', 1)
    return os.path.join(dir, '%s_%s_%s.%s' % (image_name, method, size, EXT))


def scale(imagefield, size, method='scale'):
    """ 
    Template filter used to scale an image
    that will fit inside the defined area.

    Returns the url of the resized image.

    {% load image_tags %}
    {{ profile.picture|scale:"48x48" }}
    """

    # imagefield can be a dict with "path" and "url" keys
    if imagefield.__class__.__name__ == 'dict':
        imagefield = type('imageobj', (object,), imagefield)

    image_path = resized_path(imagefield.path, size, method)

    if not os.path.exists(image_path):
        try:
            import Image
        except ImportError:
            try:
                from PIL import Image
            except ImportError:
                raise ImportError('Cannot import the Python Image Library.')

        image = Image.open(imagefield.path)

        # normalize image mode
        if image.mode != 'RGB':
            image = image.convert('RGB')

        # parse size string 'WIDTHxHEIGHT'
        width, height = [int(i) for i in size.split('x')]

        # use PIL methods to edit images
        if method == 'scale':
            image.thumbnail((width, height), Image.ANTIALIAS)
            image.save(image_path, FMT, quality=QUAL)

        elif method == 'crop':
            try:
                import ImageOps
            except ImportError:
                from PIL import ImageOps

            ImageOps.fit(image, (width, height), Image.ANTIALIAS
                        ).save(image_path, FMT, quality=QUAL)

    return resized_path(imagefield.url, size, method)



def crop(imagefield, size):
    """
    Template filter used to crop an image
    to make it fill the defined area.

    {% load image_tags %}
    {{ profile.picture|crop:"48x48" }}

    """
    return scale(imagefield, size, 'crop')


register.filter('scale', scale)
register.filter('crop', crop)
&lt;/pre&gt;

&lt;p&gt;Those filters work with imagefields, but you can pass them any object that has &lt;strong&gt;path&lt;/strong&gt; and &lt;strong&gt;url&lt;/strong&gt; properties, even a dict.&lt;/p&gt;

&lt;p&gt;Edited images will be saved under the same path as the original file.&lt;/p&gt;</description><link>http://djangosaur.tumblr.com/post/422589280</link><guid>http://djangosaur.tumblr.com/post/422589280</guid><pubDate>Tue, 02 Mar 2010 21:55:00 +0100</pubDate><category>image</category><category>resize</category><category>django</category><category>pil</category><category>thumbnail</category><category>filter</category></item><item><title>GeoDjango with Django 1.2</title><description>&lt;p&gt;I&amp;#8217;ve been struggling with trying to use geodjango with django 1.2 when all you need to do is update your database settings.&lt;/p&gt;

&lt;p&gt;If you are encountering errors like:&lt;/p&gt;

&lt;pre class="brush: python"&gt;
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'
AttributeError: 'module' object has no attribute 'GeoSQLCompiler'
&lt;/pre&gt;

&lt;p&gt;Set your DATABASE_ENGINE correctly:&lt;/p&gt;

&lt;pre class="brush: python"&gt;
'ENGINE': 'django.contrib.gis.db.backends.postgis',
&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://code.djangoproject.com/ticket/12727"&gt;Error while doing syncdb with a GIS app and django 1.2&lt;/a&gt;&lt;/p&gt;</description><link>http://djangosaur.tumblr.com/post/409277054</link><guid>http://djangosaur.tumblr.com/post/409277054</guid><pubDate>Wed, 24 Feb 2010 17:36:00 +0100</pubDate><category>geodjango</category><category>django</category><category>multidb</category></item><item><title>Looking for a domain name suggestion?</title><description>&lt;h2&gt;Computer suggestions&lt;/h2&gt;

&lt;p&gt;I use &lt;a href="http://www.visualthesaurus.com/"&gt;Visual thesorus&lt;/a&gt; to find interesting keywords and mix them with &lt;a herf="http://www.bustaname.com"&gt;bustaname.com&lt;/a&gt; to find available suggestions.&lt;/p&gt;

&lt;h2&gt;Human suggestions&lt;/h2&gt;

&lt;p&gt;I am trying &lt;a href="http://pickydomains.com/"&gt;Picky domains&lt;/a&gt; and will update this post with results.&lt;/p&gt;

&lt;p&gt;I have made the $50 payment and am waiting for further instructions.&lt;/p&gt;</description><link>http://djangosaur.tumblr.com/post/407272501</link><guid>http://djangosaur.tumblr.com/post/407272501</guid><pubDate>Tue, 23 Feb 2010 18:37:00 +0100</pubDate><category>domain name</category><category>registration</category><category>keyword</category><category>suggestion</category></item><item><title>SQL update for your django models, old school style</title><description>&lt;p&gt;Let&amp;#8217;s say you have added a new field to the profile model.&lt;/p&gt;

&lt;p&gt;Use sqlall &amp;lt;app&amp;gt; to generate the SQL.&lt;/p&gt;

&lt;pre class="brush: bash"&gt;
./manage.py sqlall profile
&lt;/pre&gt;
&lt;pre class="brush: sql"&gt;
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`) REFERENCES `auth_user` (`id`);
COMMIT;
&lt;/pre&gt;

&lt;p&gt;Using the output on line number 5, we fall back to SQL and edit the ALTER TABLE command to update the table model.&lt;/p&gt;

&lt;pre class="brush: bash"&gt;
./manage.py dbshell
&lt;/pre&gt;

&lt;pre class="brush: sql"&gt;
ALTER TABLE `profile` ADD
`new_field` integer UNSIGNED NOT NULL;
&lt;/pre&gt;

&lt;h3&gt;With django_extensions&lt;/h3&gt;

&lt;p&gt;If you have installed django_extensions, it is event easier.&lt;/p&gt;

&lt;pre class="brush: bash"&gt;
./manage.py sqldiff profile
&lt;/pre&gt;

&lt;pre class="brush: sql"&gt;
BEGIN;
-- Application: profile
-- Model: UserProfile
ALTER TABLE `profile`
	ADD `new_field` integer UNSIGNED;
COMMIT;
&lt;/pre&gt;</description><link>http://djangosaur.tumblr.com/post/360119521</link><guid>http://djangosaur.tumblr.com/post/360119521</guid><pubDate>Fri, 29 Jan 2010 22:00:00 +0100</pubDate><category>django</category><category>django-tips</category><category>sql</category></item><item><title>Always import your django settings this way!</title><description>&lt;pre class="brush: python"&gt;
from django.conf import settings
&lt;/pre&gt;


&lt;p&gt;If you don&amp;#8217;t, the settings won&amp;#8217;t contain the default values.&lt;/p&gt;

&lt;h3&gt;Django settings documentation&lt;/h3&gt;

&lt;p&gt;Here&amp;#8217;s the algorithm Django uses in compiling settings:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Load settings from global_settings.py.&lt;/li&gt;
&lt;li&gt;Load settings from the specified settings file, overriding the global settings as necessary.&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Links&lt;/h3&gt;

&lt;p&gt;&lt;a href="http://docs.djangoproject.com/en/dev/topics/settings/#default-settings"&gt;&lt;a href="http://docs.djangoproject.com/en/dev/topics/settings/#default-settings"&gt;http://docs.djangoproject.com/en/dev/topics/settings/#default-settings&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.djangoproject.com/browser/django/trunk/django/conf/__init__.py"&gt;&lt;a href="http://code.djangoproject.com/browser/django/trunk/django/conf/__init__.py"&gt;http://code.djangoproject.com/browser/django/trunk/django/conf/__init__.py&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://djangosaur.tumblr.com/post/359406403</link><guid>http://djangosaur.tumblr.com/post/359406403</guid><pubDate>Fri, 29 Jan 2010 08:08:00 +0100</pubDate><category>django</category><category>django-tips</category></item><item><title>Install django-extensions!!!</title><description>&lt;pre class="brush: bash"&gt;
pip install -e git+git://github.com/django-extensions/django-extensions.git#egg=django_extensions
&lt;/pre&gt;

&lt;h3&gt;Automatically import your applications models in your python shell&lt;/h3&gt;

&lt;pre class="brush: bash"&gt;
./manage.py shell_plus
From 'auth' autoload: Permission, Group, User, Message
From 'my_app' autoload: MyModel
&amp;gt;&amp;gt;&amp;gt;
&lt;/pre&gt;

&lt;h3&gt;Visualize your project&lt;/h3&gt;

&lt;pre class="brush: bash"&gt;
./manage.py graph_models -a -g -o my_project_visualized.png
&lt;/pre&gt;

&lt;h3&gt;ForeignKeyAutocompleteAdmin&lt;/h3&gt;

&lt;p&gt;Instead of having a select box with every FK in the admin, you have an autocomplete text input.&lt;/p&gt;

&lt;pre class="brush: python"&gt;
# profile/admin.py
from django.contrib import admin
from django_extensions.admin import ForeignKeyAutocompleteAdmin

from .models import UserProfile

class UserProfileAdmin(ForeignKeyAutocompleteAdmin):
    related_search_fields = { 
        'user': ('username',),
    }   

admin.site.register(UserProfile, UserProfileAdmin)
&lt;/pre&gt;

&lt;p&gt;Don&amp;#8217;t forget to copy the django_extensions media in your static path (for javascript and css helpers).&lt;/p&gt;

&lt;h3&gt;And more&amp;#8230;&lt;/h3&gt;

&lt;p&gt;Dive into the code, this is just the tip of the iceberg&amp;#8230;&lt;/p&gt;

&lt;h3&gt;Links&lt;/h3&gt;
&lt;p&gt;&lt;a href="http://github.com/django-extensions/django-extensions"&gt;&lt;a href="http://github.com/django-extensions/django-extensions"&gt;http://github.com/django-extensions/django-extensions&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://djangosaur.tumblr.com/post/358055866</link><guid>http://djangosaur.tumblr.com/post/358055866</guid><pubDate>Thu, 28 Jan 2010 17:16:00 +0100</pubDate><category>django</category><category>django-tips</category></item><item><title>Set MySQL storage engine to InnoDB for Django transactions</title><description>&lt;p&gt;The advantage of InnoDB tables is transactions.&lt;/p&gt;

&lt;p&gt;If you make multiple edits to the database and something goes wrong, you won&amp;#8217;t be left with half the data updated. Either everything gets written or nothing.&lt;/p&gt;

&lt;p&gt;Open your settings file:&lt;/p&gt;

&lt;pre class="brush: python"&gt;
DATABASES = { 
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '',                      
        'USER': '',     
        'PASSWORD': '',
        'OPTIONS': {
               "init_command": "SET storage_engine=INNODB",
        }   
    }   
}
&lt;/pre&gt;

&lt;h3&gt;Convert MyISAM tables to InnoDB&lt;/h3&gt;

&lt;p&gt;If your have already created your tables and they are using MyISAM, convert them.&lt;/p&gt;

&lt;pre class="brush: bash"&gt;
python manage.py dbshell
&lt;/pre&gt;

&lt;pre class="brush: sql"&gt;
SHOW TABLES;
SHOW CREATE TABLE &amp;lt;tablename&amp;gt;;
ALTER TABLE &amp;lt;tablename&amp;gt; ENGINE=INNODB;
&lt;/pre&gt;

&lt;h3&gt;Using transactions with django&lt;/h3&gt;

&lt;pre class="brush: python"&gt;
# my_app/views.py
from django.db import transaction

@transaction.commit_on_success
def my_view(request):
    ....
&lt;/pre&gt;

&lt;h3&gt;Links&lt;/h3&gt;

&lt;p&gt;&lt;a href="http://docs.djangoproject.com/en/dev/ref/databases/#creating-your-tables"&gt;&lt;a href="http://docs.djangoproject.com/en/dev/ref/databases/#creating-your-tables"&gt;http://docs.djangoproject.com/en/dev/ref/databases/#creating-your-tables&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://docs.djangoproject.com/en/dev/topics/db/transactions/"&gt;&lt;a href="http://docs.djangoproject.com/en/dev/topics/db/transactions/"&gt;http://docs.djangoproject.com/en/dev/topics/db/transactions/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Database_transaction"&gt;&lt;a href="http://en.wikipedia.org/wiki/Database_transaction"&gt;http://en.wikipedia.org/wiki/Database_transaction&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://djangosaur.tumblr.com/post/357759467</link><guid>http://djangosaur.tumblr.com/post/357759467</guid><pubDate>Thu, 28 Jan 2010 12:23:00 +0100</pubDate><category>django</category><category>django-tips</category></item><item><title>Upload a static file on tumblr</title><description>&lt;a href="http://www.tumblr.com/themes/upload_static_file"&gt;Upload a static file on tumblr&lt;/a&gt;</description><link>http://djangosaur.tumblr.com/post/356522723</link><guid>http://djangosaur.tumblr.com/post/356522723</guid><pubDate>Wed, 27 Jan 2010 20:44:57 +0100</pubDate><category>tumblr</category></item></channel></rss>
