2008-06-20

3 things I would like to see in Django 1.0

I am using Django for more than 2 years now. During several projects I made up I found that some code snippets were missing. These snippets are a common need for me and I think it's maybe the case for others.

CallTag

Just like include, but can pass parameters to it.

This snippet is incredibly useful. Before I was doing things like that:

{% with "64x64" as image_size %}
    {% with 1 as display_birthday %}
        {% include "user.html" %}
    {% endwith %}
{% endwith %}

And for each variable needed the syntax becomes more complicated. With CallTag it would be like that:

{% call include "user.html" with image_size="64x64" display_birthday=1 %}

It's a lot more readable and debuggable. Additionaly, you can "easily" add smart caching using parameters.

Inclusion tags fill not this need because you can't choose the template. Includes with "with" keyword are ugly to read and to write, so IMHO, Django really need this template tag.

New forms output control

The newforms module save lot of time validating and generating automatic forms. But when it comes to control the generated output it another story.

One reason is because the newforms module don't use the Django templating language. IMHO it's the root of the problem. This abstraction is leaky in a bad way.

It will more powerful if the newforms module use templating to render its fields. This way we could override these templates and render the forms the the way we want. Today it's too complicated to take control of the renderer of the final form inputs. Costum Fields and Widgets objects could be created at this purpose but it's not a piece of cake.

One solution is to take the form auto rendering output as a scaffold and tweak it. It's not a bad solution but I think it's possible to do better.

Query several models with an Union

As far has I know it's not possible to create a SQL Union request on several models as if there is only one.

The possible solution is to create a SQL View into the database and a Django model that match this view but it feel strange to use a database trick to handle this (it's maybe a good idea after all).

It could be nice if Django ORM provide a way to create a Union QuerySet on several tables. Here is an exemple code of what I imagine:

class Post(models.Model):
    title = models.CharField(blank=False)
    author = models.ForeignKey(User, related_name='user_posts')
    body = models.TextField()
    
class Comment(models.Model):
    title = models.CharField(blank=False)
    author = models.ForeignKey(User, related_name='user_comments')
    body = models.TextField()
    
class Product(models.Model):
    name = models.CharField(blank=False)
    owner = models.ForeignKey(User, related_name='user_products')
    description = models.TextField()
    
class Content(models.Union):
    class Meta:
        union_models = (Post, Comment, Product, )
        union_fields = ('title', 'author', 'body' 'content_type',)
        union_fields_rename = ('name as title', 'owner as author',)
    
contents = Content.objects.filter(author_id=1)