Five Things I Hate About Django.

The five things I hate about * meme seems have died down, and memes, should not be allowed to die.

Of course I love Django, and have bet very heavily on it. But we do not know a topic, until we know it warts, so here you go. The listing is in no particular order, so sorry no numbering.

Ajax with Django is hard:

Most of the Django Community has decided that bundling Javascript helpers with a python framework is bad idea. Though I can understand the reasoning, (argued here and rebuttal), that Javascript is so basic that you can not be expected to not know it, I can not agree with it. SQL is as basic as Javascript, and yet we have ORM for abstracting away the common and the tedious.

Of Course, with simplejson, and a good Javascript library, you can build Ajax apps fast and with only a minimal amout of fuss. And yet switching between Python and Javascript, twice every hour is a huge time drain. Eg. I put commas after the last element in Python arrays, with JS this would work in FF, but fail with weird errors in IE.

Lack of identity map:

If you get the same row from the DB twice using Model.objects.get, you will get two different objects. Apart from the performance problems of two DB queries, when only one should have done, when you update one of them, the other does not get updated, and you will have interesting things happening in your application. And if you update both of them, you might write two inconsistent changes to the DB.

Look at this code for example.

  1.  
  2. See this code
  3. In [2]: from django.contrib.auth.models import User
  4. In [3]: usr1 = User.objects.create_user(‘ram’, ‘demo@demo.com’, ‘demo’)
  5. In [4]: usr2 = User.objects.get(username=‘ram’)
  6. In [5]: usr3 = User.objects.get(username=‘ram’)
  7. In [6]: user2 == user3
  8. —————————————————————————
  9. NameError                                 Traceback (most recent call last)
  10. In [7]: usr2 == usr3
  11. Out[7]: True
  12. In [8]: usr3.username = ‘not_ram’
  13. In [9]: usr3.save()
  14. In [10]: usr2.username
  15. Out[10]: u‘ram’
  16. In [11]: us3.username
  17. —————————————————————————
  18. NameError                                 Traceback (most recent call last)
  19. In [12]: usr3.username
  20. Out[12]: ‘not_ram’
  21. In [13]: usr2 == usr3
  22. Out[13]: True
  23.  

Whether Sessions are browser length/persistent are set sitewide:

You can set whether you want sessions to be browser length/persistent using SESSION_EXPIRE_AT_BROWSER_CLOSE in settings.py. But you can not set them per user, without mucking with django internal. This might seem a minor annoyance, yet this is something which you need to do for every app, as the remember me, function will not work without this.

Newforms is very limited:

Let us say you want the Form to contain a varible number of fields. How can you define the NewForms class to do your biddings.

  1.  
  2. from django import newforms as forms
  3. class MyForm(forms.Form):
  4.     foo = froms.CharField()
  5.         bar = froms.CharField()
  6.  

This can only create a form with a fixed number of fields. While there are ways to generate forms with variable number of fields, (generate the Form class programatically), they are not easy or well documented. (Remind me to write such tutorial sometime.)

Bonus question: How can you generate a form with same form elements multiple (and variable number) times, ala what happens with edit_inline?

Settings mixes application configuration which should be public and passwords, which should be private:

If I am distributing an app MIDDLEWARE_CLASSES is something which I would assume users would not (generally) modify. Similarly, in most of the cases, INSTALLED_APPS, would also be something which users would not change, (unless you are distributing standalone_apps). This means, I want to source control settings.py. But settings.py also contain my DB setiings, and SECRET_KEY, which means, I cannot source control settings.py.

And while we are at it, can we refactor settings.py, so it works without

  1.  
  2. os.environ[‘DJANGO_SETTINGS_MODULE’] = ’settings’
  3.  

Bonus:

Two things which used to bug me but no more. 1. You cannot extend Models - Well now you can if you use queryset-refactor, or soon can if you are on trunc. 2. Url configuration using regexes. - Now they have two problems. joke notwithstanding, mapping URLs to views is one problem where regexes fit the problem beautifully. With less that 50 lines of code, you can manage a large number of views, and Url patterns.

Now stay tuned for Five things I love about Django

12 comments ↓

#1 Duc on 04.18.08 at 1:48 am

Regarding your settings.py problem, you can have a file in your project called localsettings.py which contains only your db user/password. Then in settings.py, remove your db info and add code to import localsettings.py. That way you can source control settings.py without source controlling local_settings.py.

#2 Alex on 04.18.08 at 2:14 am

you would be smart not to do ‘remember me’ through long running sessions - instead use a authentication hash that can be used to re-login after a long time.

#3 Lorenzo Bolognini on 04.18.08 at 2:47 am

AJAX is hard with Django is complete FUD

#4 Dan on 04.18.08 at 3:19 am

While it’s very new, I’d be interested to see what you think of TurboGears2.

#5 shabda on 04.18.08 at 4:05 am

@Lorenzo “AJAX is hard with Django is complete FUD” I am sorry you thnk of it this way. I absolutely LOVE Django, so I guess I meant “AJAX is hard with Django, compared to how easy Django makes other things”

#6 Lorenzo Bolognini on 04.18.08 at 4:26 am

What i really mean is that by getting completely out of your way (and just giving you some convenience functions to serialize Models to json) it actually makes things easier.

And DOM scripting doesn’t get any easier than jQuery! ;)

L.

#7 Five things I love about Django. — The 42 Topics Blog on 04.18.08 at 6:05 am

[…] ← Five Things I Hate About Django. […]

#8 EvanC on 04.18.08 at 8:40 am

You’ve completely missed:

1) Lack of migrations in the ORM 2) Poor REST support

#9 troy on 04.18.08 at 9:05 am

if you are on trunc

I think you mixed up trunk and trac :)

#10 shabda on 04.18.08 at 10:44 am

@EvanC

  1. Lack of migrations in the ORM: I am pretty good with SQL, so firing a few ALTER TABLE .. SET DEFAULT .. is not a big deal for me, so its not what I hate.
  2. Poor REST support: Try http://code.google.com/p/django-rest-interface/

@troy:

He he, :)

#11 Julian on 04.18.08 at 2:36 pm

Lack of GROUP BY in the ORM!!! This is driving me nuts… why isn’t that included???

As for newforms with variable number of fields.. yes you can generate the class programatically… but compared to how easy Django makes other things.. that is just crappy.

#12 Vanchuck on 06.23.08 at 5:28 pm

@Julian: in my (fairly limited) experience with various ORMs, I have yet to see one that supports GROUP BY. I think the rationale behind this is that ORMs return model objects from the database; however if you are using GROUP BY, you typically are performing a SUM(), COUNT(), or other arithmetic operation, in which case the result doesn’t fit the concept of a strict model– since a sum or count of matching items is not a property of the object, but a property of that object’s relations to other objects.

Not to say it wouldn’t be useful, but I guess the use cases are limited enough and don’t fit the ORM concept enough to make it a priority to implement. IMHO, such queries are best executed “raw” (or through a database abstraction layer).

@EvanC + Shabda: Database Migration is kinda similar– Sometimes having a tool to do things for you is more trouble that just doing it manually. I’m a fan of versioning SQL structure dumps, then running a simple diff to remind myself of what needs to be changed when migrating. If the changes require some data manipulation, it should be done in a migration script anyway– plugging along with incomplete or partially-converted data on a live site, while you finish the modifications on the shell or something like phpPgAdmin, is never a good idea!

I just don’t see how a tool could (reliably) automatically know how to do all that for you.

Leave a Comment