Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django PositiveNormalizedDecimalField
Some weeks ago I was working on a Django project that was using some FloatFields where they should not be used, the database backend was Postgres 8.4. If you are not aware of it, FloatFields are turned into double precission types, that means 64-bit floating-point numbers. Those fields were storing values that didn’t need such precission. For example I don’t think you need them to store items’ weights, do you? I migrated those fields to DecimalFields using an average (max_digits=7, decimal_places = 2), which means a total of 7 digits, with 2 decimals. Of course I had to migrate the database, doing some manual alters, as Django-south was not being used. By default DecimalFields render the number with all its decimal digits, which was not what I was looking for. I needed a decimal field that: By default render decimal values normalized using non-scientific notation. See Decimal("10.400").normalize() Only allowed positive values I could have used floatformat built-in filter, but that would have supposed to change hundreds of different templates. Even though grep and sed are a big allies, the process would be quite error prone. I simply needed a centralized solution. Obviously It was time to create a custom model … -
Django PositiveNormalizedDecimalField
Some weeks ago I was working on a Django project that was using some FloatFields where they should not be used, the database backend was Postgres 8.4. If you are not aware of it, FloatFields are turned into double precission types, that means 64-bit floating-point numbers. Those fields were storing values that didn’t need such precission. For example I don’t think you need them to store items’ weights, do you? I migrated those fields to DecimalFields using an average (max_digits=7, decimal_places = 2), which means a total of 7 digits, with 2 decimals. Of course I had to migrate the database, doing some manual alters, as Django-south was not being used. By default DecimalFields render the number with all its decimal digits, which was not what I was looking for. I needed a decimal field that: By default render decimal values normalized using non-scientific notation. See Decimal("10.400").normalize() Only allowed positive values I could have used floatformat built-in filter, but that would have supposed to change hundreds of different templates. Even though grep and sed are a big allies, the process would be quite error prone. I simply needed a centralized solution. Obviously It was time to create a custom model … -
Leaving NASA
This has been a hard post to write. I was delighted that on January 3rd, 2005 I started my first day working for the National Aeronautics and Space Administration (NASA). While I wasn't working on science efforts, I was at least contributing to the cause. In 2005 I was introduced by co-worker Chris Shenton to Python, which became my favorite programming language ever. I also learned tools like Zope, Plone, and Django. Over the past five years, I've met a lot of fascinating people in and around the agency, a list that seems endless in size and scope. That includes astronauts, scientists, engineers, developers, managers, and so much more.This meant so much to me, and maybe because my first memories of television as a child were the moon landings of the early 1970s. I dreamed as a child of being an astronomer or astronaut, and sometimes I plot how I would redo my life to fit these dreams if I got a second childhood.In the past year I've had some incredible opportunities present themselves to me. I've been presenting frequently on Django and Pinax. I've had the singular honor of writing course material for Holdenweb, LLC on behalf of the … -
Speeding up Django unit test runs with MySQL
Here are a couple of tips to speed up unit test runs on Mac OS X and Linux when running MySQL. -
Speeding up Django unit test runs with MySQL
When I'm developing Django sites, my database of choice is usually PostgreSQL. However, lots of clients use MySQL. And there lies a problem: table creation on MySQL seems to be an order of magnitude slower on Mac OS X than on Linux. This makes repeated unit test runs extremely painful. I researched this a little bit a while ago, and noticed that it had been reported as a bug in the MySQL tracker. At the time, there were no fixes or workarounds. A recent update, however, has revealed the use of the skip-sync-frm option. Put it in your MySQL config file in the [mysqld] section for a quick speedup: [mysqld]default-table-type=innodbtransaction-isolation=READ-COMMITTEDdefault-character-set=utf8skip-sync-frm=OFF Of course, nothing in this life is free, as Daniel Fischer explains in a comment: The reason why it's slower on Mac OS X than on Linux is that on Mac OS X, fcntl(F_FULLFSYNC) is available, and mysqld prefers this call to fsync(). The difference is that fsync() only flushes data to the disk - both on Linux and Mac OS X -, while fcntl(F_FULLFSYNC) also asks the disk to flush its own buffers and blocks until the data is physically written to the disk.In a nutshell, it's slower because … -
Speeding up Django unit test runs with MySQL
When I'm developing Django sites, my database of choice is usually PostgreSQL. However, lots of clients use MySQL. And there lies a problem: table creation on MySQL seems to be an order of magnitude slower on Mac OS X than on Linux. This makes repeated unit test runs extremely painful. I researched this a little bit a while ago, and noticed that it had been reported as a bug in the MySQL tracker. At the time, there were no fixes or workarounds. A recent update, however, has revealed the use of the skip-sync-frm option. Put it in your MySQL config file in the [mysqld] section for a quick speedup: [mysqld]default-table-type=innodbtransaction-isolation=READ-COMMITTEDdefault-character-set=utf8skip-sync-frm=OFF Of course, nothing in this life is free, as Daniel Fischer explains in a comment: The reason why it's slower on Mac OS X than on Linux is that on Mac OS X, fcntl(F_FULLFSYNC) is available, and mysqld prefers this call to fsync(). The difference is that fsync() only flushes data to the disk - both on Linux and Mac OS X -, while fcntl(F_FULLFSYNC) also asks the disk to flush its own buffers and blocks until the data is physically written to the disk.In a nutshell, it's slower because … -
Speeding up Django unit test runs with MySQL
When I'm developing Django sites, my database of choice is usually PostgreSQL. However, lots of clients use MySQL. And there lies a problem: table creation on MySQL seems to be an order of magnitude slower on Mac OS X than on Linux. This makes repeated unit test runs extremely painful. I researched this a little bit a while ago, and noticed that it had been reported as a bug in the MySQL tracker. At the time, there were no fixes or workarounds. A recent update, however, has revealed the use of the skip-sync-frm option. Put it in your MySQL config file in the [mysqld] section for a quick speedup: [mysqld]default-table-type=innodbtransaction-isolation=READ-COMMITTEDdefault-character-set=utf8skip-sync-frm=OFF Of course, nothing in this life is free, as Daniel Fischer explains in a comment: The reason why it's slower on Mac OS X than on Linux is that on Mac OS X, fcntl(F_FULLFSYNC) is available, and mysqld prefers this call to fsync(). The difference is that fsync() only flushes data to the disk - both on Linux and Mac OS X -, while fcntl(F_FULLFSYNC) also asks the disk to flush its own buffers and blocks until the data is physically written to the disk.In a nutshell, it's slower because … -
Tracking changes to fields in Django
-
Tracking changes to fields in Django
A common practice we have used over at Disqus is tracking changes on a model (explicitly) in order to determine if certain actions need to take place when that instance is updated. We tend to use it like "if changed" database triggers. This has been very useful in situations like marking a commen... -
Reactions to "Stupid Template Languages"
My blog post on Stupid Template Languages has had some excellently crafted responses by Armin Ronacher, Mike Bayer, and Steve Holden. I respect and admire each of these developers, and their combined projects have made a real impact on my career. I'm happy that our community is large enough to have a difference of opinion, delighted that our base language of Python allows us the power to play with different options so easily, and hope that we can debate our differences of opinion this year at PyCon. The first round of drinks are on me!Response of Armin Ronacher, creator of Jinja2 and other projectsResponse of Mike Bayer, creator of Mako and SQL AlchemyResponse of Steve Holden, Chairman of the Python Software Foundation and creator of PyCon -
Django forms ChoiceField with dynamic values…
… or how to get a dynamic drop-down list with Django forms The problem: Lets say that you need a form with a drop-down list that have dynamic values. With Django this can be done simple and fast, but if you are new you may get yourself into a trap. In a standard form(with static [...] -
chrisdickinson’s wilson at master – GitHub
chrisdickinson’s wilson. Einen noch vor dem Mittagessen, denn das Framework orientiert sich stark an Django, und da ich ja Django-Fan bin, ist das sicherlich einen eigenen Link wert. -
django-output-validator released
finally got round to cleaning up my Django validator app... -
South 0.7.3 released
I've released South 0.7.3. It's only a bugfix release; the release notes can be found here. -
South 0.7.3 released
I've released South 0.7.3. It's only a bugfix release; the release notes can be found here. -
Finding Out-of-Sync Packages Across Servers with Fabric
We have several servers that are supposed to have the same packages installed, but often get out of sync due over time. To make it easier to find these out-of-sync packages and servers, I wrote a quick fabric script to do the checking for me. The server environment A few things are assumed in this post:  You use pip Your project is installed within a virtualenv None of this will work without pip, as it relies on pip’s freeze command, and it will work really badly if you have multiple projects on the server and no virtualenvs. Starting the fabfile.py You may already be using Fabric for some automation, so you could skip to the next section. We are going to set up our “fabfile,” or list of commands that Fabric will use. Create an empty file named fabfile.py and insert this at the top: from __future__ import with_statement from fabric.api import env, run, settings, hide from fabric.decorators import hosts, runs_once venv = "/home/websites/.virtualenvs/coolsite/" env.user = 'webdev' env.hosts = [ '192.168.1.10', '192.168.1.11', '192.168.1.12', '192.168.1.13', ] This imports the pieces we need, sets the path to find the virtual environment and the connection information. Getting the list of packages pip has a nice command, freeze, that will list all the … -
Porting from App Engine's webapp to django-nonrel
Hey, you may have noticed it already, we've finally finished our top-secret article called Running Pure Django Projects on Google App Engine :). We thank Wesley Chun for his help, expertise and time so we could get the article published on App Engine's articles section. Summarized the article goes through several steps explaining in depth how to port an App Engine webapp app over to django-nonrel with some additional notes on useful Django features. At the end of the article we summarize the advantages of using django-nonrel over other approaches and what awaits you in the future. But enough words, just read the article :) Leave a comment -
URLs on multi-lingual sites: A solution
Let me tell you about two problems: Your site is localized into multiple languages, but the words in its URLs can only be in one language. For example, the URL for the "About Us" page will always be "/about-us/" no matter what language you're viewing the site in. Django's language switching is kind of crappy. You have to post to a view to change the language, and it's hard to expose all the site's translations to search engines for indexing. Ladies and gentlemen, the solution is at hand: Transurlvania! Transurlvania is a small suite of URL-related tools for multi-lingual sites with an incredibly hilarious name. It's up on github here: http://github.com/trapeze/transurlvania Transurlvania has two major components: url translation, and language-in-URL URL Translation URL Translation lets you localize your URLs without a lot of headache. It means the natural language words in your URLs can always be legible to your visitors. How easy is it? Well, here's a regular urlconf module: from django.conf.urls.defaults import * urlpatterns = patterns('', url(r'^about-us/$', 'about_us', name='about_us'),) To make that URL translatable, you would transform the module like so: from django.utils.translation import ugettext_noop as _ from transurlvania.defaults import * urlpatterns = patterns('', url(_(r'^about-us/$'), 'about_us', name='about_us'),) Now run make messages and … -
Fuzzy testing with assertNumQueries
When assertNumQueries is overkill... -
Peewee, a lightweight Python ORM
For the past month or so I've been working on writing my own ORM in Python. The project grew out of a need for a lightweight persistence layer for use in Flask web apps. As I've grown so familiar with the Django ORM over the past year, many of the ideas in Peewee are analagous to the concepts in Django. My goal from the beginning has been to keep the implementation simple without sacrificing functionality, and to ultimately create something hackable that others might be able to read and contribute to. -
App-Centric Django Development Part 2: App Factory
Make it easy Django makes it easy to start an app with the command ./manage.py startapp. However the app template that it creates is very simple and it creates it within the current project. We need to make the process of creating an independent app that easy, with a much better template. To make it that easy, we created a script to ask for information and merge it with an application template. Our script and template is on github and called django-app-skeleton. The App Factory The script to create the new app is a highly modified version of a script from Eric Florenzano. The script asks for a bit of information and then substitutes placeholders within the app template (a directory tree, not a single file) with that information. The substitution is done both within files and on the names of files. There isn’t much information that you need to gather, currently the script requests: Application Name Usually something like “django-somethingorother”. This is the name on PyPI. Package Name Just the “somethingorother” part of the application name. There are places in the default template that want to import the package, and this is the value. Author For the setup.py script, you need an author. Destination Directory Where is the script going … -
Celery: mini tutorial
Configuració del servidor rabbitmq El servidor Rabbitmq és qui rebrà les ordre d'execució de les tasques i les enviarà als workers que han de fer la feina i rebrà el resultats de les operacions de manera que el procés que ha donat l'ordre d'execució de la tasca el pugui fer servir. És important destacar que a l'hora de donar l'ordre d'execució podem decidir si el resultat s'ha de guardar o bé s'ha de descartar. Si el resultat no és necessari (pensem per exemple en l'enviament d'un e-mail) llavors convé marcar la tasca per a que el resultat es descarti. sudo aptitude install rabbitmq-server Els paquets nous següents s'instal·laran: erlang-os-mon{a} erlang-snmp{a} rabbitmq-server 0 paquets a actualitzar, 3 a instal·lar, 0 a suprimir i 5 a no actualitzar. Es necessita obtenir 1398kB d'arxius. Després del desempaquetat s'utilitzaran 3187kB. Esteu segur de voler continuar? [Y/n/?] y Obté:1 http://es.archive.ubuntu.com/ubuntu/ maverick/main erlang-snmp i386 1:13.b.3-dfsg-2ubuntu3 [581kB] Obté:2 http://es.archive.ubuntu.com/ubuntu/ maverick/main erlang-os-mon i386 1:13.b.3-dfsg-2ubuntu3 [77,4kB] Obté:3 http://es.archive.ubuntu.com/ubuntu/ maverick/main rabbitmq-server all 1.8.0-1ubuntu2 [739kB] S'han obtingut 1398kB en 2s (480kB/s) S'estan preconfigurant els paquets... S'està seleccionant el paquet erlang-snmp prèviament no seleccionat. (S'està llegint la base de dades … hi ha 118931 fitxers i directoris instal·lats actualment.) S'està desempaquetant erlang-snmp … -
Peewee, a lightweight Python ORM - Original Post
Edit Rewrote peewee from the ground up Edit, Jul 24, 2011 added support for Postgresql Edit, June 8, 2011 added support for MySQL For the past month or so I've been working on writing my own ORM in Python. The project grew out of a need for a lightweight persistence layer for use in Flask web apps. As I've grown so familiar with the Django ORM over the past year, many of the ideas in Peewee are analagous to the concepts in Django. My goal from the beginning has been to keep the implementation simple without sacrificing functionality, and to ultimately create something hackable that others might be able to read and contribute to. Weighing in at about 1000 lines of code, Peewee doesn't come close to matching Django's ORM (15K LOC) in terms of API cleanliness or functionality, but it does hit many of the basic use-cases for an app that needs lightweight persistence and querying. This has definitely been one of the most rewarding projects I've worked on! Benchmarks In terms of speed, peewee is generally 25% faster than django when creating rows or grabbing simple lists of objects. Peewee is 77% faster than Django for simple one-row … -
Peewee, a lightweight Python ORM - Original Post
Edit I rewrote peewee from the ground up. The query examples in this post are no longer supported. Edit, Jul 24, 2011 added support for Postgresql and MySQL (in addition to SQLite). Edit, June 8, 2011 added support for MySQL For the past month or so I've been working on writing my own ORM in Python. The project grew out of a need for a lightweight persistence layer for use in Flask web apps. As I've grown so familiar with the Django ORM over the past year, many of the ideas in Peewee are analagous to the concepts in Django. My goal from the beginning has been to keep the implementation simple without sacrificing functionality, and to ultimately create something hackable that others might be able to read and contribute to. Weighing in at about 1000 lines of code, Peewee doesn't come close to matching Django's ORM (15K LOC) in terms of API cleanliness or functionality, but it does hit many of the basic use-cases for an app that needs lightweight persistence and querying. This has definitely been one of the most rewarding projects I've worked on! Benchmarks In terms of speed, peewee is generally 25% faster than django when … -
Peewee, a lightweight Python ORM - Original Post
Edit I rewrote peewee from the ground up. The query examples in this post are no longer supported. Edit, Jul 24, 2011: added support for Postgresql and MySQL (in addition to SQLite). Edit, June 8, 2011: added support for MySQL For the past month or so I've been working on writing my own ORM in Python. The project grew out of a need for a lightweight persistence layer for use in Flask web apps. As I've grown so familiar with the Django ORM over the past year, many of the ideas in Peewee are analagous to the concepts in Django. My goal from the beginning has been to keep the implementation simple without sacrificing functionality, and to ultimately create something hackable that others might be able to read and contribute to. Weighing in at about 1000 lines of code, Peewee doesn't come close to matching Django's ORM (15K LOC) in terms of API cleanliness or functionality, but it does hit many of the basic use-cases for an app that needs lightweight persistence and querying. This has definitely been one of the most rewarding projects I've worked on! Benchmarks In terms of speed, peewee is generally 25% faster than django when …