These are good questions. web2py does not have something like Django
managers and you have to achieve the same goal differently. This is
because of different in design philosophy.

Django has an ORM, web2py has a DAL. In Django a model maps a table
into an object so you can override the methods of that object.
web2py prefers to be a lower lever than that. the web2py DAL
expressions map 1-to-1 into SQL queries. For example we do not have a
save() methods. We do have an insert() method. You cannot override it
because it does not belong to the table (model) but it belong to the
database. (technically you could override it but you would be doing
for all tables).

What in Django you achieve by modifying the model, in web2py you
achieve my modifying the forms. Forms can be associated to two
functions: onvalidation, onaccept. The former is executed after
validation before any database IO, the latter is executed after
database IO.

You can define these functions in the model (they are just functions,
not class methods) but you still need to be specific about which form
they apply to (onvalidation=...).

Hope this helps.

Massimo


On Jul 29, 3:18 pm, __future__ <wrigh...@gmail.com> wrote:
> 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