Looking at my post, I can see where not knowing Django would be an
impediment to understanding... :)

Let me try again (and please bare with me because I am *not* an expert
on Django).

Django has these things called Managers:

http://docs.djangoproject.com/en/dev/topics/db/managers/

from the Django docs:

class Manager

A Manager is the interface through which database query operations are
provided to Django models. At least one Manager exists for every model
in a Django application.

You can create your own Managers and use them with your models.  You
can create custom QuerySets (filters basically) and use them with your
Managers.

In the example I gave, he creates a function today():

from datetime import datetime, timedelta
def today():
    now = datetime.now()
    start = datetime.min.replace(year=now.year, month=now.month,
        day=now.day)
    end = (start + timedelta(days=1)) - timedelta.resolution
    return (start, end)

Then a custom QuerySet (EventQuerySet) that extends the base QuerySet
with a today method which uses the function he defined above.

self.filter(creation_date__range=today()) # __range is part of the
django filtering syntax

Then he creates a custom Manager (EventManager) and assigns it to
'objects' in his model:

class Event(models.Model)
#....
objects = EventManager()

So now when he calls Event.objects he is really calling an instance of
the EventManager()

He does one more thing though.. All models have a default save method
which is called explicitly to commit changes.  He overrides the
inherited default save method with this one:

    def save(self, **kwargs):
        Event.objects.filter(latest=True,
            creator=self.creator).today().update(latest=False)
        super(Event, self).save(**kwargs)

So now whenever an Event object is saved, this code will execute
regardless of where it happens because save is a model method.

I think I explained that correctly (possibly not well :)

What would be the best (or most DRY) way to do something like this in
web2py?

Thanks again to all for indulging my questions...


On Jul 29, 1:51 pm, Fran <francisb...@googlemail.com> wrote:
> On Jul 29, 6:51 pm, __future__ <wrigh...@gmail.com> wrote:
>
> > So can I create a custom filter like the EventQuerySet in the example
> > and then apply it in the controller?  I assume I will have to use this
> > anywhere an event might get an update?  Can I use it with crud?
>
> I don't quite follow the example (I'm not familiar with Django) but
> what I think you're looking for is:
> crud.settings.create_onvalidation = lambda form: mycustomfilter(form)
>
> F
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to