Archive for the ‘Django’ Category

DiggPaginator update 11

Here's an update (link goes to DjangoSnippets) to the paginator code I posted a while back (link goes to the old version). The main thing is the deprecation warning, which is now gone.
In particular:

Previously, a custom base class was used that implemented a stateful, page-aware paginator. Now that Django's new Paginator class does essentially [...]

Django: Post-QSRF Aggregation 1

I'm in the process of upgrading critify to the current SVN trunk, with QSRF now included. So far, everything is going smoothly, and I believe I am about done.
At one point in the project I need to let the database do a SUM()/COUNT() calculation. Not wanting to write the complete SQL manually, I had previously [...]

Yet another paginator (digg style) 17

This code will throw deprecation warnings in newer Django checkouts - see the Paginator Update post for an improved version that should work with the recent trunk.
These days I had to implement pagination with Django for the first time. Actually, that's not entirely true - but thus far it had always been simple cases where [...]

Fun with timezones in Django & MySQL 2

My very first post to the Django Mailinglist was a question about the TIME_ZONE setting (and embarrassingly, from the look of that Google Groups page, the second one as well). Malcolm was nice enough to explain things, and that was that.
Until today, when I ran into another time zone related problem: A custom MySQL query [...]

Revisited: Common fields, model subclassing 0

One of the first issues I ran into when I started with Django was the lack of support for model inheritance. Not true model inheritance on the SQL level, but just the possibility to inherit from other models in Python to reuse field definitions (i.e. copy them). Finally, I came up with this snippet, which [...]

Dynamically generating a ModelForm 0

If you need formfield_callback:

ModelFormMetaclass = type(forms.ModelForm)

Form = ModelFormMetaclass(
'DynForm', (forms.ModelForm,),
{'Meta': type('Meta', tuple(), {'model': model})},
formfield_callback=field_callback)

Recursion in Django templates 6

Recently, someone asked in #django about outputting recursive structures, say a forum or comment thread. Although this could be considered a presentation logic issue, the smartest thing is most likely to prepare the data in your view accordingly. Besides, Django's template language doesn't support recursion. Or does it?
There are no macros, or a call/function [...]

FuzzyDates, or: One Django Model Field, multiple database columns 2

A while ago, I implemented support for fuzzy dates in critify, i.e. incomplete dates like "January 2008", "2008" or "3Q 2008". I decided two use two separate columns on the database level, one storing the date itself, the other a "precision". Based on the latter I then can determine which parts of the date value [...]

Decorating urlpatterns 5

One thing I wanted for a while was the ability to basically apply something like @login_required to a bunch of urlpatterns in one go, instead of having to decorate each and every view manually. I finally spent some time looking through the Django source, and I came up with the following:

from django.core.urlresolvers import RegexURLPattern
from django.conf.urls.defaults [...]

Python: What I learned today 0

Actually, the title of this post should be Python: What I learned this week, as I've been trying to debug this problem for at least that long. What problem, you ask?
First let me say that I'm writing this down mostly as a recap and for my own reference, but if it can help a lonesome [...]