In this episode we continue to build out our events application. Before building our models any further, however, we decide to install the Django admin site so that we can explore the models. After doing that, we create a custom Manager, and even a custom QuerySet so that it becomes extremely easy to filter our QuerySet to just the events happening today.
Why Customize the Manager?
Simply put, it saves us from having to re-type code that we would otherwise have to re-type over and over again. Not only that, but once we save code by applying a Manager to a certain Model--if we set things up correctly--Managers can be re-used on other Models as well. For example, if we had another model with a field called creation_date, we could easily apply our newly-created EventManager to that Model and begin using the .today() method, as described in the screencast.
For more information about Managers, and their advantages over other alternatives like classmethods, see James Bennett's blog post on the subject.
Simon Willison also has a great post describing the use of chaining with Django's ORM, where he ends up creating a custom Manager and QuerySet. This is an excellent example of the kind of chaining that we will be doing later on in the project, when we build out our views.
Comments - 9 people have already said something. Join the discussion.
nono said…
Great, I realy like screencasts. However I would turn down the pace of this screencast a little bit more and explain a bit more why your subclass and how this works. Beginners don't realy understand subclassing and how it all works! For you this is second nature. For the beginner its WOW what is he doing and what are the implications.
Björn said…
Your screencasts are just great. I'm particularly interested in your workflow. Even though it's not particularly related to django, I'm looking forward to seeing how you do deployment to the production server and keep the versions under, er, control.
I hope the comments will become a good arena for discussions for when things go too fast. The custom QuerySets were new to me, for one.
Darren said…
Great screencast, nice pace easy on the ears. Keep them coming, this can only help the django community grow.
I wonder about Nono's comment are we teaching programming or the concepts of the framework?.
Also where has the QT version gone - and are you going to get these into itunes anytime soon.
Eric said…
I got a bit lost on the custom QuerySet. Why do we need that and also the custom Manager?
Nono said…
@Darren
I don't think it hurt to also have some programming concepts. A lot of people come from spaghetti php and keep thinking that way.
ps. I love these screencasts!
Björn said…
@Eric: As I understand it, the manager allows you to get every Event for today:
Event.objects.today()
The QuerySet however enables you to slap a 'today()' on ANY Event queryset:
Event.objects.all().today()
or
Event.objects.filter(description__startswith='G').today()
etc.
With just the manager, it's limited to the beginning of the chain of queryset filters.
I'm still getting my mind around this myself, incidentally, perhaps someone can give a better description of how it all fits together and what the benefits are more specifically?
Eric Florenzano said…
@nono Sorry about the pace of the 'cast :( One of the problems inherent with doing *everything* from the simple stuff to the hard stuff in one screencast is that some episodes progress too slowly for some people, and some will go too fast for others. The one suggestion that I can give is to ask questions about what is too fast and/or go back and re-watch sections.
@Björn You got it! If you implement the method only on the manager, you have to start off the chain with that item. The one downside about doing your own QuerySet subclass is that you _have_ to also write a custom Manager subclass as well to produce those QuerySet instances.
Scot Hacker said…
Hrm, I can't seem to get episode 3 (.mov version) to load. It's timing out today.
Skylar Saveland said…
The showmedo link for the flash version is not working.