On 7/2/06, Ahmad Alhashemi <[EMAIL PROTECTED]> wrote:
> I agree that explicitness is better. But I think implicit filtering
> here is not that bad for two reasons. The first is, as I said, it is
> the rule not the exception. The second is that it makes it extremely
> easy to start your application as a single site application then turn
> it into a multi-site application.

Django *does* already provide a way to do this: the CurrentSiteManager
in django.contrib.sites[1]. Adding it to an existing model requires no
changes to your database, and will require minimal changes to code
that interacts with that model (and no changes whatsoever if you make
it the default manager, which may or may not suit your use case).

> The same way, you don't have to add code to tell the view that the
> table contains records for many weblogs and we are only interested in
> one weblog at a time. You will almost never run a query without
> specifying the weblog_id because the weblogs are completely seperate,
> and as I said before, you can almost put each weblog's data in its own
> database, but you don't want to do that.

There's no clean way to make Django automatically do all the filtering
for you because, as Malcolm pointed out, Django can't read your mind
and guess that you're always going to want to filter on a certain
condition. However, the database API can still make your life a lot
easier ; drop an ID or a slug or something in the weblog URLs which
can be used to uniquely identify a weblog, and then filter on the
related QuerySet. For example, if you had an Entry model related via
ForeignKey to a Weblog model, the following would fetch the latest
five entries:

def latest_five_entries(request, slug):
    weblog = Weblog.objects.get(slug__exact=slug)
    latest_five = weblog.entry_set.all()[:5]
    return render_to_response('latest_entries.html', { 'latest_five':
latest_five })

Basically, once you've looked up the Weblog object, the 'entry_set'
QuerySet attached to that Weblog will give you all the filtering you
need; because it's a QuerySet, you can call all(), filter(), count(),
etc. or any other query method on it.

[1] http://www.djangoproject.com/documentation/sites/#the-currentsitemanager

-- 
"May the forces of evil become confused on the way to your house."
  -- George Carlin

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---

Reply via email to