December 2011
20 posts
Faster or lazier pagination
Or how to avoid slow counts in Postgres.
This is a paginator that does not do a count and it all started with this blog post. Essentially Arecibo was getting hammered by the number of the requests that were being sent to it. It wasn’t the requests that were the problem, is the listing of them.
I was doing all the usual stuff, vacuuming and making indexes. However those counts were still...
The power of Q
Q objects are a great tool in Django.
Q objects are lumbered with a name that’s very hard to Google. Down there with F objects and Plone’s event. You’ll probably come across Q objects when you need to complicated lookups such as not, or and so on. But these objects are so useful.
Complex lookups
You can make Q objects for the lookups you’d like. For...
Javascript inline forms outside of contrib.admin
This recipe follows on from the last two and shows how to do the addition of inline fields to a model form, outside of contrib.admin.
Note: this is excerpt a recipe from cleardjango. You can download the source from SVN. Once you’ve checked it out, do: python tools/configure.py -s 72 to install and run this recipe.
In the last two recipes, we’ve seen how to add inlines through the admin...
Adding Javascript to contrib.admin inline fields
This recipe follows on from the last by adding Javascript to the inline fields of a model in the contrib.admin.
Note: this is excerpt a recipe from cleardjango. You can download the source from SVN. Once you’ve checked it out, do: python tools/configure.py -s 71 to install and run this recipe.
By default the inlines in contrib.admin are a fixed number. In each case Django adds in three...
Setting up inline forms in contrib.admin
This recipe covers how to set up inline forms in contrib admin and how to show foreign keys in those inline forms.
Note: this is excerpt a recipe from cleardjango. You can download the source from SVN. Once you’ve checked it out, do: python tools/configure.py -s 70 to install and run this recipe.
Two different models are often used, one related to another. These are normally shown to...
Tuning the count method on a queryset
Some alternate ways to manipulate the pagination of Django query sets.
The other week I faced a real slow down on Arecibo due to a high number of errors being generated by a few other sites. At the time I started to find out how bad Postgres is compared to MySQL in terms of performance when doing a SQL count. MySQL does more work to make that faster.
In Arecibo, we present a list of all the...
The lack of magic in Django
…or why Rails and Zope 2 were too magical.
There’s been a bit of a discussion recently about magic from James Bennett and others. Also one of my friends recently had a play with Ruby and came to some conclusions. I did a bit of work in Rails a while back and it never did much for me I found large applications confusing and hard to debug.
If you ask me why I don’t like Rails,...
Forms, forms, forms
Some questions answered about Django forms.
…again based on questions that keep cropping up in the IRC channels.
1. How do I add or change fields on a ModelForm?
The exact same way you do on a normal Form. If the name of the field matches a field that already exists on the model the form is using, it will override the default.
class Cool(forms.ModelForm):
new =...
Features of a good Django plugin
Well, my opinion on them anyway.
A while back I wanted to allow openid logins on my site. A simple problem which a few plugins released to the Django community make easier. However there’s quite a few of them (we list 4, there’s more) and none of them quite work the way I want.
So what’s wrong with them? Most of them define front end templates or particular models or a...
Read only fields in models
Supposing you have a field in your model and you don’t want it to be changed, ever.
How would you do that? Well actually you can’t from Django, which we’ll see in a minute. Here’s a script that should work for most people. Basically, set up a signal (or a custom save method) so that when your object is saved it compares the old to new and then figures out if it can be...
Class based views
We discussed these at a recent Vancouver Django (May 2009) meetup and it was also a pattern I used on a recent project.
Traditionally in Django a URL points to a function. So something like this is probably what you familar with:
from django.conf.urls.defaults import patterns
urlpatterns = patterns('',
(r'^hello-function/$', 'recipe_9.views.hello_function')
)
And a view that looks like...
Useful Django API's
Two useful Django API’s that I’ve found today.
Model to Dict
A useful little fella hiding away in the forms module. Ever want a dictionary of your model and it’s fields? Then this one will give you that. Useful for mapping a model into a big string. For example in a recent project we are sending a message about the facility.. pass in a facility into a string and you’ll...
Caching and signals
Adding in cached objects on signals.
Was reading this great talk on Django and performance from EuroDjangoCon. There are some good points in there, one I wanted to play with quickly was the caching framework and signals.
I’m a big fan of signals and the idea of setting and deleting the cache as objects change was just something I had to quickly play with. Nothing too revolutionary here,...
Django, Malnutrition, SMS and Kenya
How I ended up doing one of most interesting and important projects I’ve been involved with.
A while back I got home from dinner to find a quick Skype chat from a friend, Nate Aune that basically went “you free to go to Kenya to do some Django - oh and you have to leave on Sunday” (that was in about 4 days time). The response was “is this for real?”. But as it turns...
Turning off django signals
While I’m on the subject, how to turn off signals that you don’t want on.
The first and most obvious signal to turn off is the one asking you for a username and password on syncdb, gosh that’s annoying. You can do this by calling:
python manage.py syncdb --noinput
But if you want to do this in Python you can:
from django.contrib.auth import models as auth_app
from...
Signal vs overriding save
I’m a big fan of signals and wonder why people are so adverse to using them, preferring overriding save instead.
Probably every day on the Django IRC channel, someone asks about how to set a user on a model or about setting the time automatically. Fortunately thanks to this excellent article, we are all familiar with the former. The latter is easy to do as well, without having to resort to...
Content mirror and this site
How this site is configured and some more details on content mirror.
I’ve been asked for this a couple of times, so here’s some details on how Content Mirror works on this site.
This site’s main editing interface for editors (myself) is done in Plone. Plone is mirroring it’s content into a Postgres database, which is being synced to a Django site, you are then reading it...
Keeping user profiles in sync
Django provides a user profile as a way to extend a users information. This isn’t created automatically for you however when you create a new User, but it is pretty simple to do this.
The easiest way to do this is to use signals. By listening to the post_save signal on users, you can create a user profile on a save.
When the user object is deleted, the profile will be deleted, because the...
Django at PyCon
A wrap up of the happenings at PyCon.
Sadly I didn’t get to PyCon this year. In fact I haven’t ever been to PyCon, just what used to be the “Python Conference” which I think has been overtaken by PyCon.
Looking at the chat and IRC logs makes me realise how many people I know there and wish I’d been able to go. Next year. After being around the Zope, then Plone, then...
Site launched
Djangozen is now live in some sort of state, hopefully working.
There’s nothing more boring than that first “oh this site is live” post. Usually because the normal developer person has gone into marketing mode and really thinks the other people care about the site. I know you care. How could you not with new Django sites going live every day.
Well here’s another.
It...