Posts tagged: Django

Creating a Custom Authentication Backend using Django

Today I have posted a new tutorial on djangorocks.com ‘Creating a Custom Authentication Backend‘.

The example created allows you to login to your Django application using your IMAP mail server to check your username & password.  Perfect if you are creating a webmail client or address book – and more I’m sure.

The tutorial is not as long as I would have hoped, but its rather difficult to add to when it really is quite a simple thing to implement.  Hope it helps.

PostgreSQL, how about you don’t make migrating from MySQL more complicated?

I am currently working on building a new site which is migrating from an existing one.  Front-end work mostly complete, now to creating the admin.

The Problem

Today, a really quite annoying “feature” of PostgreSQL raises its head and takes up far too much time to resolve.  For some background, the data is from an old site built in PHP & MySQL, being migrated to Django & PostgreSQL. This has been working fine, the frontend works and that’s great.

To maintain the old database id’s we inserted everything en-mass into the new database. This in itself was not the problem.  The problem arose when starting to adding new row and are expected auto-increment to the end.

Just a simple query, nothing overly complicated here.

INSERT INTO table (title) VALUES ('my_title');

However, instead of actually inserting into a new row at the end, it started working from id 1. This throws up errors whenever the id already existed (this differs from MySQL). Worse still, on some occasions started to delete items from the end of the database.

The Solution

So, what’s going on? I was initially expecting it to be a problem with my code although but, after plenty of testing, that wasn’t the case.

The primary key wasn’t updating the auto-increment value as expected. Here was the simple fix (change field names where necessary). This will reset the internal counter to the next available number.

SELECT setval('"table_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "table";

If you are using Django  you can generate the SQL by running, it does not run the automatically but you can pipe the response directly into the psql tool.

python manage.py sqlsequencereset appname

Very simple, but this does not follow the MySQL (less control over increments and automatically finding the next available number). Then again SQLite also does things differently and no doubt others as well.

It really pays to be aware of some of the fundamental, often unexpected, differences between databases and frameworks. Embrace change, don’t fear it. Do your research, is it better?, does it make for a logical change? But, don’t embrace unnecessarily.

Django 1.2 and 1.1.2 released

Django 1.2 is now out, along with a bug-fix release for 1.1.  It looks like a bug has already been found, but that’s to be expected as the audience grows.  Better these problems are found early on.

Django 1.2 has a bunch of new features;

  • Support for multiple databases
  • Model validation
  • Improved CSRF protection
  • Messages framework
  • Object-level permissions
  • Permissions for anonymous users
  • Relaxed requirements for usernames
  • E-mail backends
  • “Smart” if tag
  • Template caching
  • Natural keys in fixtures
  • Fast failure for tests
  • BigIntegerField
  • Improved localization
  • readonly_fields in ModelAdmin
  • Customizable syntax highlighting
  • Syndication feeds as views
  • GeoDjango
  • JavaScript-assisted handling of inline related objects in the admin
  • New now template tag format specifier characters: c and u

Final, Estimated Django 1.2 release date

Over the last few weeks, Django contributors have been working hard finding and fixing bugs from 1.2 Beta 1.  The RC release has been set at around May 3, and final release May 10.  There are a couple of patches still to be completed, more here.

That being said, it also means that I am only a few weeks (hopefully) from completing one of my sites, mentioned in my last news post.  Although the site is not yet finished and there is still quote a lot to do, a lot of the code is in place, so I can start writing the tutorials.

A design is being completed and finalised by a friend of mine.  He doesn’t yet have his own site, but when he does and if I remember, I will add a link.  Things that will be used in this site;

This is the first site I have coded using the 960 grid system (although 2nd version) and this is also the first design Oli has done using it, if he gets some time, hopefully he will get me a quick post explaining some of the design process.

Also, Facebook connect code has been completed……for Django 1.1.  Although there should be no need to change much, there maybe some things I change, ie use of the new Messages framework.  This will be decided in the near future.

The Django & MooTools tutorials I promised.

No, they are not here yet, and I havn’t forgotten.  With Django 1.2 being out shortly I decided to redo parts of the site to reflect changes in the framework, some of which were mentioned in a previous post.  I would also like to review all of the new features, some changes and remove anything which will be Deprecated shortly.

I also stated the tutorials would cover MooTools.  Due to the fact I need to learn how to use jQuery in the fairly near future, I’ve decided I will use this for starting.  Granted the tutorials may not be the best, but will be far easier for beginners to understand, being one myself.  I will probably write the same things in MooTools over time as it is (at the moment) still my framework of choice.

Dansette