Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Getting Random objects from a Queryset in Django
When providing related or suggested info to the user in a website, it’s a common practice to obtain a random set of items. To do this in django the “usual” way is: Book.objects.all().order_by('?')[:10] The above code, sorts all books in a random order and then picks the first 10 objects. This approach is not really [...] -
Django aggregation and a simple GROUP BY
I love Django's aggregation framework. It very successfully abstracts the common aggregration tasks into a Pythonic syntax that sits extremely well with the rest of the ORM, and the documentation explains it all without a single reference to SQL. But sometimes that very abstraction gets in the way of working out what you want to do. One example of this happened to me today when I needed to do a sum of values grouped by a single value on a model - in SQL terms, a simple GROUP BY query. The documentation is very clear about how to do aggregations across an entire QuerySet, and annotations across a relationship. So you can, for example, easily do a sum of all the 'value' fields in a model, or a sum of all the 'value' fields on a related model for each instance of the parent model. But the implication is that these are the only things you can do. So I was left wondering if I had to create a dummy related model just to contain the unique values of the field I wanted to group on. In fact, what I wanted to do was clearly documented, but because of the … -
Django aggregation and a simple GROUP BY
I love Django's aggregation framework. It very successfully abstracts the common aggregration tasks into a Pythonic syntax that sits extremely well with the rest of the ORM, and the documentation explains it all without a single reference to SQL. But sometimes that very abstraction gets in the way of working out what you want to do. One example of this happened to me today when I needed to do a sum of values grouped by a single value on a model - in SQL terms, a simple GROUP BY query. The documentation is very clear about how to do aggregations across an entire QuerySet, and annotations across a relationship. So you can, for example, easily do a sum of all the 'value' fields in a model, or a sum of all the 'value' fields on a related model for each instance of the parent model. But the implication is that these are the only things you can do. So I was left wondering if I had to create a dummy related model just to contain the unique values of the field I wanted to group on. In fact, what I wanted to do was clearly documented, but because of the … -
Gunicorn / Django Debian init script
This is a simple Debian init.d script for my self-hosted Django/Gunicorn websites. Note that it runs inside a virtualenv (my installed Django versions are different in each virtualenv). If you have questions: comment this post! #!/bin/sh ### BEGIN INIT INFO # Provides: seismic_web # Required-Start: $local_fs $syslog # Required-Stop: $local_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Gunicorn processes for seismic_web ### END INIT INFO # www-data is the default www user on debian USER=www-data NAME="seismic_web" GUNICORN_RUN="gunicorn_django" # Confdir: the Django project inside the virtualenv CONFDIR="/home/thomas/seismic_web/seismic_web" VENV_ACTIVATION=". ../bin/activate" RETVAL=0 # PID: I always name my gunicorn pidfiles this way PID="/tmp/gunicorn_"$NAME".pid" # source function library . /lib/lsb/init-functions start() { echo "Starting $NAME." cd $CONFDIR; su -c "$VENV_ACTIVATION; $GUNICORN_RUN" $USER && echo "OK" || echo "failed"; } stop() { echo "Stopping $NAME" kill -QUIT `cat $PID` && echo "OK" || echo "failed"; } reload() { echo "Reloading $NAME:" if [ -f $PID ] then kill -HUP `cat $PID` && echo "OK" || echo "failed"; fi } case "$1" in start) start ;; stop) stop ;; restart) reload ;; reload) reload ;; force-reload) stop && start ;; *) echo $"Usage: $0 {start|stop|restart}" RETVAL=1 esac exit $RETVAL -
Deploying Django project with gunicorn and Nginx
Easy and fast deployment with gunicorn - a WSGI Python HTTP server and super fast and scalable Nginx server. -
Django Staging Server: Apache Configuration
In this post, I'd like to talk a little about how my staging server (called Kaylee, after the character from Firefly), is configured to run the multiple Django projects that I have on the go. A lot of the other articles I have read on this subject use mod_python but my server is configured to [...] -
Installing GeoDjango Dependencies with Homebrew
Installing GeoDjango Dependencies with Homebrew. brew update && brew install postgis && brew install gdal -
Installer Django dans un environnement Python virtuel avec pip, virtualenv et virtualenvwrapper
Ce billet résume les étapes nécessaires pour installer un ou plusieurs environnements de développement Django fonctionnels, portables et faciles à maintenir sous Mac OS X. Même si Django est un framework relativement simple à installer, lorsqu’il s’agit de développer plusieurs projets mettant en œuvre différentes versions de ce dernier ou de librairies tierces nécessaires pour assurer son bon fonctionnement, le casse-tête peut rapidement devenir ingérable si l’on ne prend pas garde à bien isoler le contexte applicatif dans un environnement dédié, isolé du reste du système. Concrètement, imaginons que j’ai deux projets Django : Le projet A utilise Django 1.2-DEV, django_toolbar en version 0.8.3 et Python 2.6 Le projet B utilise Django 1.1 et Pinax en version 0.6 tournant avec Python 2.5 Impossible dans ces conditions d’utiliser une version unique de chacune des librairies installées sur le système (et non, l’utilisation de liens symboliques et de préfixes n’est pas une solution acceptable sur le moyen/long terme). Aussi, découvrant progressivement la richesse de l’écosystème Python, je n’ai pas manqué de m’extasier devant la puissance et la simplicité d’outils tels que pip, virtualenv et virtualenvwrapper pour répondre à ces questions. pip, un installeur de paquet simple et efficace D’aucuns de ceux qui … -
django-site-gen, a tool for automating site creation
Most django sites I create have quite a lot in common. Beyond the handful of files generated by django-admin startproject, my projects all have a database, wsgi file, apache and nginx confs, static media and templates. All these building blocks of a site vary very little from project-to-project. django-site-gen allows you to automate the creation of the stuff that doesn't vary. -
Extending Settings Variables with local_settings.py in Django
I discovered this hacky way to use the local_settings trick to extend and/or override values in the main Django settings file today. Some projects use a "reverse" version of the local_settings trick (which is explained below), whereby the main settings file becomes settings_local.py or something similar, which first imports settings.py, and then extends or overrides [...] -
stackoverflow, stackexchange 2.0 oh my!
The problem It is quite amusing, how the experts-exchange killer stackoverflow.com is becoming something similar. Ever since StackOverflow started, people wanted to make their own sites for similar Q&A types of sites. Various topics could benefit from the similarly formatted Q&A sites. Unfortunately the SO team has not really been very open to open-sourcing it's source code. First they tried to make some bucks from selling it, thanks to the business mind of Joel Spolsky. However not all developers were eager to shell out $130/month for having their own little stackoverflow clone. So then, SO team, figured that they will never make big money by just having 4 paid sites (as that's all they managed to sell I think). What to do, what to do? This is where stackexchange 2.0 comes in. They together with a community (who are mostly developers) will "approve" the topics for the sites they think are worthy, and will host it themselves. Advanges? Ad revenue. Disadvantages? You can't really start your own. Even if your idea is great it may get downvoted by the community. After all - they are a community of developers, not say mechanics. The solution Free Django based Open Source Q&A … -
Idea for a simple task queue
cue is a simple queue abstraction along the lines of the command pattern, and provides django apps with a way to decouple the creation of a request from its execution. -
Damn foreign keys, stealing our jobs and women
Django has support for Generic Foreign Keys, which let you reference one model instance from another, without knowing up-front what that model type is. The classic use for something like this is for a commenting system; you need generic foreign keys – or something like them – because you wouldn't want a commenting system that only worked with a single model. If you have ever used generic foreign keys in Django, you will know that it is not quite transparent to the developer; a little effort is required to manage the various content types. I'll present here an alternative method to achieve this late binding of foreign keys that doesn't require storing the type of the object (as generic foreign keys do) and is completely transparent to the developer. I'm sure I'm not the first to think of this method, but I haven't yet seen it used in other Django projects. Rather than store the type of object in a separate field, we can create a new model for each foreign key type we want to reference. For example; lets say we have a Rating model, and we want to rate Articles and Images – we could do this by … -
Damn foreign keys, stealing our jobs and women
Django has support for Generic Foreign Keys, which let you reference one model instance from another, without knowing up-front what that model type is. The classic use for something like this is for a commenting system; you need generic foreign keys – or something like them – because you wouldn't want a commenting system that only worked with a single model. If you have ever used generic foreign keys in Django, you will know that it is not quite transparent to the developer; a little effort is required to manage the various content types. I'll present here an alternative method to achieve this late binding of foreign keys that doesn't require storing the type of the object (as generic foreign keys do) and is completely transparent to the developer. I'm sure I'm not the first to think of this method, but I haven't yet seen it used in other Django projects. Rather than store the type of object in a separate field, we can create a new model for each foreign key type we want to reference. For example; lets say we have a Rating model, and we want to rate Articles and Images – we could do this by … -
Damn foreign keys, stealing our jobs and women
Django has support for Generic Foreign Keys, which let you reference one model instance from another, without knowing up-front what that model type is. The classic use for something like this is for a commenting system; you need generic foreign keys – or something like them – because you wouldn't want a commenting system that only worked with a single model. If you have ever used generic foreign keys in Django, you will know that it is not quite transparent to the developer; a little effort is required to manage the various content types. I'll present here an alternative method to achieve this late binding of foreign keys that doesn't require storing the type of the object (as generic foreign keys do) and is completely transparent to the developer. I'm sure I'm not the first to think of this method, but I haven't yet seen it used in other Django projects. Rather than store the type of object in a separate field, we can create a new model for each foreign key type we want to reference. For example; lets say we have a Rating model, and we want to rate Articles and Images – we could do this by … -
The Mess Django’s In?
In the Mock It! blog, Malthe Borch writes that Django’s innards are a disaster. The comments are as good as the article. Money quote: [If you] take a peek under the hood, your impression of Django will change dramatically: The code is awful. [Django's] got a terrific story if you’re willing to drink the kool-aid [...] -
My Idea Of The Django Blogging App™
I am not going to talk about yet another Django-based blogging engine in this post. There are a number of blogging apps which try to be like turn-key solutions, like a WordPress blog. I have skimmed through the code of many such apps, but haven’t used one yet. Some of them are really high quality [...] -
Optimizing Django: tricks for faster page loads
By reducing the file size of your CSS, JavaScript and images, as well as the number of unnecessary browser requests made to your site, load time of your applications pages can be drastically reduced, not to mention the load on your server. Yahoo have created a list of the 35 best practices to speed up your website, a recommended read for any web developer. I wanted to summarize a few I recently implemented in a Django application. In a nutshell: Serve compressed static media files. This one is obvious, the smaller the file size, the quicker it is to download. Once a browser has already downloaded the static media, it shouldn't have to download it again. This is what actually happens, as the server will respond with 304 Not Modified, which means that the cached file is still up-to-date. However, the HTTP request is still sent. This is unnecessary and should be avoided by setting the HTTP Expire header to a date in the future. When files are updated, you need to give them a new URL so the browser will request them. This is referred to as versioning. A common scheme is to use the modification time/date of the … -
Optimizing Django: tricks for faster page loads
By reducing the file size of your CSS, JavaScript and images, as well as the number of unnecessary browser requests made to your site, load time of your applications pages can be drastically reduced, not to mention the load on your server. Yahoo have created a list of the 35 best practices to speed up your website, a recommended read for any web developer. I wanted to summarize a few I recently implemented in a Django application. In a nutshell: Serve compressed static media files. This one is obvious, the smaller the file size, the quicker it is to download. Once a browser has already downloaded the static media, it shouldn't have to download it again. This is what actually happens, as the server will respond with 304 Not Modified, which means that the cached file is still up-to-date. However, the HTTP request is still sent. This is unnecessary and should be avoided by setting the HTTP Expire header to a date in the future. When files are updated, you need to give them a new URL so the browser will request them. This is referred to as versioning. A common scheme is to use the modification time/date of the … -
Optimizing Django: tricks for faster page loads
By reducing the file size of your CSS, JavaScript and images, as well as the number of unnecessary browser requests made to your site, load time of your applications pages can be drastically reduced, not to mention the load on your server. Yahoo have created a list of the 35 best practices to speed up your website, a recommended read for any web developer. I wanted to summarize a few I recently implemented in a Django application. In a nutshell: Serve compressed static media files. This one is obvious, the smaller the file size, the quicker it is to download. Once a browser has already downloaded the static media, it shouldn't have to download it again. This is what actually happens, as the server will respond with 304 Not Modified, which means that the cached file is still up-to-date. However, the HTTP request is still sent. This is unnecessary and should be avoided by setting the HTTP Expire header to a date in the future. When files are updated, you need to give them a new URL so the browser will request them. This is referred to as versioning. A common scheme is to use the modification time/date of the … -
Announcing django-audit
As part of some work at 2degrees on ensuring an audit trail for our Django models, we have been working on a project to add under-the-hood auditing to the Django Model class.We've published an early alpha of this code on launchpad, and hope to have a final release for this package around the end of May to coincide with the deployment of our internal code.If you can't wait for the documentation, then checkout the code and have a read of the source. Otherwise documentation will be coming along shortly. -
GSOC 2010 – I’m updating the Django Test Suite
I’ve been accepted to work on Django’s Test Suite during the Google Summer of Code this year. I’ll be working to improve the runtime and uniformity of the test suite, mainly by converting existing doc tests to unit tests. This isn’t particularly glamorous, but it should be a chance to get intimately familiar with the [...] -
Primeiro projeto com Python, Django e MongoDB
Estou desde o começo deste ano acompanhando o hype dos bancos de dados orientados a documento, também chamado de NoSQL (embora não goste muito deste termo). Como bom curioso, resolvi testar algumas variações como o CouchDB e MongoDB, onde acabei me identificando mais com o segundo. Gostei da praticidade e rapidez em fazer as coisas com ele, também a performance me chamou muito a atenção, tem uma boa biblioteca Python e a documentação é muito objetiva, com exemplos de uso. Esses pontos foram decisivos para a escolha. Com um banco de dados orientado a documentos, não existe a necessidade de criar o modelo das tabelas, basta definir um documento (estilo JSON) e inserir na base. Esses documentos podem ter campos a mais ou a menos dependendo da necessidade e tudo pode ser indexado e encontrado facilmente. Estou trabalhando em um projeto onde é necessário indexar uma grande quantidade de informações e documentos. O problema é que muitos documentos não seguem um padrão e existe a necessidade de estar alimentando a base a todo instante com novas informações. Já desenvolvi modelos de busca neste estilo utilizando o Xapian, que é uma excelente ferramenta. Porém a performance fica bastante prejudicada quando a … -
Django Model Templates (sans-Denormalisation)
I came across an interesting problem recently, while trying to model the structure of a university course in Django. The model needed to represent the notion of a university module, which can be taught over a number of semesters and/or years, by different people, and with different students each time round. Some information remained common [...] -
Official nonrel/NoSQL support in Google Summer of Code!
This is great news for Django developers who want to use non-relational DBs: Alex Gaynor got accepted for adding non-relational DB support to Django's ORM during Google Summer of Code! This means that we now have to make sure that the ORM changes are flexible enough to build a common API for all nonrel DBs, so code can be reused everywhere without any modifications (or, at least between DBs with similar capabilities). Once we have official nonrel support in Django we can move on to the next phase of Django-nonrel: building an indexing layer which supports denormalization and many other features that go beyond the DB's native capabilities and which you'd normally implement by hand. This topic has been discussed in the recent DjangoDose NoSQL panel and you surely noticed how controversial it is. Well, we don't have to decide which solution is the best one - both have their pros and cons. Instead, our planned Django-nonrel indexing layer can be an optional package (outside of Django core) that you can use on top of the low-level backend if you want more productivity, at the cost of having to understand what the technical limitations behind this method are (they're the …