There are many ways.
If you are using crud, you can do

def fix_latest(form):
    db(db.event.user=auth.user.id).update(latest=False)
    form.vars.latest=True

def index():
    db.event.latest.readable=db.event.latest.writable=False
    form=crud.create(db.event,onvalidation=fix_latest)
    return dict(form=form)

Massimo


On Jul 29, 8:21 am, __future__ <wrigh...@gmail.com> wrote:
> Thanks for all of the replies  This is a very responsive group!
>
> I'm still not clear on how to replicate some of the Django
> functionality here but at least I think I understand how many-to-many
> works in web2py.
>
> I had seen the tagging example before but something about that method
> bothers me.  It reminds of a Clarion DOS app we use at work from 25+
> years ago. There was a many-to-many relationship between models &
> options.  The solution was to store a list of option ids in a field in
> the model table.  The idea was almost identical except instead of |1|2|
> 3|4| it was just a regular comma separated list 1,2,3,4.
>
> Another question about the example I am trying to convert:
>
> In the django app, the 'last' field in the Event model is used such
> that only one 'event' per user can have latest=True.  They accomplish
> this by using django's manager class, a custom filter and overriding
> the save method of the model.
>
> So in the model file
>
> 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)
>
> class EventQuerySet(QuerySet):
>     def today(self):
>         return self.filter(creation_date__range=today())
>
> class EventManager(models.Manager):
>     def get_query_set(self):
>         return EventQuerySet(self.model)
>
>     def today(self):
>         self.get_query_set().today()
>
> class Event(models.Model):
> #....
>     objects = EventManager()
>
>     def save(self, **kwargs):
>         Event.objects.filter(latest=True,
>             creator=self.creator).today().update(latest=False)
>         super(Event, self).save(**kwargs)
>
> So before it saves any new event, it sets any other events of that
> user for today() as latest=false. I am sure there is an easier way to
> do that but that is what it is in the example.
>
> What would be the web2py way of doing something like this?
>
> A working example of this tutorial site is still up at:
>
>  http://startthedark.com.
>
> A link to the complete source:
>
>  http://github.com/ericflo/startthedark/tree/master
>
> Thanks again to all for answering my previous question.
>
> On Jul 28, 6:41 pm, Yarko Tymciurak <yark...@gmail.com> wrote:
>
> > You should be aware that this "tagging" style is ok, and works - but the way
> > it works may not be the right thing for your application.
>
> > If you have limited references (say ~20 or so per a "many")  then this is
> > simple, efficient, and you can probably manage your many-to-many this way.
>
> > What this does is stores a set of table references (integers, basically) as
> > text delimited by "|", so you would have a reference in this style which
> > looks (for example) like
>
> > "|1|27|153|225|"
>
> > and your code would need to parse it.
>
> > To have "many" to "many", you would just have cross references like this,
> > one of the tag-style  "many" definitions in each of two tables.
>
> > I like the traditional way to do this, and that is to define a table to hold
> > the many, and there are advantages depending on what you want to do.
>
> > For example:
>
> > db.define_table('location',
> >            Field('name'))
> > db.define_table('transportation',
> >           Field('name'))
>
> > # many-to-many table - holds what transportation is available where
> > db.define_table('available',
> >           Field('location', db.location),
> >           Field('transport', db.transportation))
>
> > Now you can define sets for operations like this:
>
> > transport_list = ((db.location.id == db.available.location)
> >                 & (db.transportation.id == db.available.transport))
> > for row in transport_list.select():
> >     print row.location.name, row.transport.name
>
> > On Tue, Jul 28, 2009 at 2:42 PM, Fran <francisb...@googlemail.com> wrote:
>
> > > On Jul 28, 5:47 pm, __future__ <wrigh...@gmail.com> wrote:
> > > > I am already confused about how to implement the Django style many-to-
> > > > many relationship in web2py.
>
> > > The Many<>Many support native to Web2Py is the Tagging-style:
> > >http://www.vimeo.com/2720410
>
> > > 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