Django E-Commerce

May 25th, 2010

Pakt publishing have released a new Django book called Django 1.2 E-Commerce, written by Jess Legg. I'll have a copy soon and post a review here.

In the meantime they are offering a free chapter of the book; Chapter 2 Setting up Shop in 30 minutes.

 

Exciting Python Developer Job

May 20th, 2010

Meebo, the instant messenger in your browser company, are seriously looking for Python developers right now to work in Mountain View, California. From what I can gather they are expanding, and are building a number of sites in the Django framework.

 

Agnostic or Atheist?

May 16th, 2010

Alright. Here is my second attempt at VBlogging. I'm getting better at talking to the camera, which I find difficult to do. Still can't do it in one cut though!

It's on the subject of Agnosticism and Atheism. It's probably old territory for most people interested in this subject matter, but hopefully somebody will find it interesting…

 

Powered by Ubuntu

May 9th, 2010
Powered by Ubuntu

<fanboy>Powered by Ubuntu</fanboy>

 

Round the houses, Linux video editing

May 3rd, 2010

Since I've upgraded to Ubuntu 10.04, I figured this would be a good time to play with video editing, which is something I've been wanting to play with for a while. I suspect that video editing could be a killer app for Linux, and there are a few packages to chose from. Kdenlive looked to be the most feature rich, but I'm afraid I found it buggy and prone to crashing, so I settled on OpenShot, which has a clean, simple interface and didn't crash on me once.

Here's the video I put together:

If you know the answer, please don't post it straight away – to give others a chance to figure it out…

 

No audio in Flash on Ubuntu 10.04

May 2nd, 2010

Upgrading to Ubuntu 10.04 went remarkably smoothly, with the exception that audio in Flash was erratic. Sometimes it worked, sometimes it didn't. Since I use my PC as an entertainment system, this was pretty much a deal-breaker.

Googling for it got me no where. Apparently “no audio in flash” is the Ubuntu equivalent of “i have a headache” – it's a vague symptom that has multiple possible causes. Long story short, the problem was that I had both Flash 9 and Flash 10 installed. Firefox appeared to pick one at random (as far as I could tell), but audio only worked with 10. The solution was to delete Flash 9 – which I couldn't do from the package manager, because it turned out I had Flash 9 installed locally.

If this sounds familiar, type about:plugins in to the Firefox address bar. If you see two entries for Flash, then give the following a try:

rm ~/.mozilla/plugins/libflashplayer.so

This is probably the result of some tinkering I did before upgrading to 10.04. I doubt this will be a problem with a fresh install.

 

Damn foreign keys, stealing our jobs and women

May 1st, 2010

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 generating a ArticlesRating model and a ImagesRating model with appropriate foreign keys. The easiest way to do this is with a function that returns a parameterized class definition.

Here's a snippet of code from a project I'm working on, that does just that:

rating.py

from django.db.models import Model, ForeignKey, IntegerField, Count, Avg
from django.db import IntegrityError
from django.contrib.auth.models import User

def make_rating_model(rated_model, namespace):

    class Rating(Model):

        user = ForeignKey(User)
        rated_object = ForeignKey(rated_model)
        vote = IntegerField(default=0, blank=True, null=False)

        class Meta:
            abstract=True
            db_table = u'rating_%s_%s' % (namespace, unicode(rated_model).lower())
            unique_together = ('user', 'rated_object')

        def __unicode__(self):
            return u"%s's rating of %s" % (self.user.username, unicode(self.rated_object))

        # Rest of the methods snipped for brevity
        # Contact me if you would like the whole class

    return Rating

This isn't a model definition, rather it is a function that create a model definition. You can call it multiple times to return a Rating model for each object you want a rating for. The function, make_rating_model takes two parameters; the name of the model you want to rate, and a string that is used to generate the table name, to avoid naming conflicts.

To create a rating object you would import ratings in your models.py file and add the following:

class ArticleRating(ratings.make_rating_model('Article', 'mysite')):
    pass

class ImageRating(ratings.make_rating_model('Image', 'mysite')):
    pass

Now if you syncdb you will get two completely independent models with essentially the same interface – which means you can write code that works equally well with model instances of either type.

This method doesn't quite replace generic foreign keys; if you don't know until runtime what model to reference, or if you require the objects to be in a single table, then you will still need generic foreign keys, but in my experience this is rarely the case.

 
Search for Posts
2013
 
2012
 
2011
 
2010
 
2009
 
2008
 
2007
 
2006
 
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan