Re: models, peeking to next record - maybe
On 2015-02-07 22:05, Vijay Khemlani wrote: > The direct solution would be something like this in your view > > events = Event.objects.order_by('event_date') > > event_tuples = [] > last_date_seen = None > > for event in events: > if last_date_seen: > date_difference = event.date - last_date_seen > else: > date_difference = None > > event_tuples.push((event, date_difference)) In the past I've created something like this as a filter, which lets it be fairly generic. I also make it a generator rather than keeping them all around. Something like (I don't have the exact code on hand) from django import template register = template.base.Library() @register.filter(name="pairwise") def pairwise(iterable, final=True): i = iter(iterable) a = next(i) for b in i: yield a, b a = b if final: yield a, None -tkc -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20150209214649.4ef17717%40bigbox.christie.dr. For more options, visit https://groups.google.com/d/optout.
Re: models, peeking to next record - maybe
Alternatively use the 'latest()' method of the django ORM to figure out which is the latest event: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#latest 2015-02-08 2:05 GMT+01:00 Vijay Khemlani: > The direct solution would be something like this in your view > > events = Event.objects.order_by('event_date') > > event_tuples = [] > last_date_seen = None > > for event in events: > if last_date_seen: > date_difference = event.date - last_date_seen > else: > date_difference = None > > event_tuples.push((event, date_difference)) > > (date_difference would be a timedelta object) > > On Sat, Feb 7, 2015 at 10:02 PM, Lachlan Musicman wrote: >> >> My initial suggestion would be to utilise the "order by" option, and >> order by date. >> >> Then, on each model have a find_next function and a until_next function. >> >> The first would work out which was the next event, the other the time. >> They could be coupled, but keeping them separate means you can utilise >> them later if you need to. >> >> cheers >> L. >> -- >> "This is a profound psychological violence here. How can one even >> begin to speak of dignity in labor when one secretly feels one's job >> should not exist?" >> >> On the Phenomenon of Bullshit Jobs, David Graeber >> http://strikemag.org/bullshit-jobs/ >> >> >> On 8 February 2015 at 09:40, Richard Bowden wrote: >> > Got an problem that I am struggling to solve.. >> > >> > I have: >> > >> > class Address(models.Model): >> > name = models.CharField() >> > event_date = models.DateTime() >> > >> > >> > event1, 25/01/2015 >> > event2, 27/01/2015 >> > event3, 05/02/2015 >> > >> > and so on… >> > >> > what I am trying to figure out is, how to calculate the days between >> > each >> > event. >> > >> > so event2, would be 2 days, event3 would be 9 days etc. >> > >> > want to end up with something like this that is passed to the template >> > event1, 25/01/2015 >> > event2, 27/01/2015, 2 >> > event3, 05/02/2015, 9 >> > >> > >> > its the mechanics I am having trouble working out, my initial though was >> > to >> > have a def on the model that accepted a datetime object. Somehow in a >> > loop i >> > would get the diatomite from the next event and pass it in and calc days >> > on >> > self.event_date… cant think how to do it. >> > >> > has anyone else needed something like this? or am I thinking about it >> > wrong.. >> > >> > Thanks >> > >> > -- >> > You received this message because you are subscribed to the Google >> > Groups >> > "Django users" group. >> > To unsubscribe from this group and stop receiving emails from it, send >> > an >> > email to django-users+unsubscr...@googlegroups.com. >> > To post to this group, send email to django-users@googlegroups.com. >> > Visit this group at http://groups.google.com/group/django-users. >> > To view this discussion on the web visit >> > >> > https://groups.google.com/d/msgid/django-users/63b0691e-34b1-4634-bf29-640583f22bae%40googlegroups.com. >> > >> > For more options, visit https://groups.google.com/d/optout. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-users+unsubscr...@googlegroups.com. >> To post to this group, send email to django-users@googlegroups.com. >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/CAGBeqiOF4-or%3DWY4DGbsQY1eFUY0bKR09sTVED9dEUc6tNUY9Q%40mail.gmail.com. >> For more options, visit https://groups.google.com/d/optout. > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CALn3ei1g-gMx6cqvQ9in8vq%2BkhLd9d6ccHefWKxba-vwpeW9KA%40mail.gmail.com. > > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFgu0xgOv7mn2etRH1nL1c3E0NrPmjmZFDbToQcOnEHaUsn7vw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: models, peeking to next record - maybe
The direct solution would be something like this in your view events = Event.objects.order_by('event_date') event_tuples = [] last_date_seen = None for event in events: if last_date_seen: date_difference = event.date - last_date_seen else: date_difference = None event_tuples.push((event, date_difference)) (date_difference would be a timedelta object) On Sat, Feb 7, 2015 at 10:02 PM, Lachlan Musicmanwrote: > My initial suggestion would be to utilise the "order by" option, and > order by date. > > Then, on each model have a find_next function and a until_next function. > > The first would work out which was the next event, the other the time. > They could be coupled, but keeping them separate means you can utilise > them later if you need to. > > cheers > L. > -- > "This is a profound psychological violence here. How can one even > begin to speak of dignity in labor when one secretly feels one's job > should not exist?" > > On the Phenomenon of Bullshit Jobs, David Graeber > http://strikemag.org/bullshit-jobs/ > > > On 8 February 2015 at 09:40, Richard Bowden wrote: > > Got an problem that I am struggling to solve.. > > > > I have: > > > > class Address(models.Model): > > name = models.CharField() > > event_date = models.DateTime() > > > > > > event1, 25/01/2015 > > event2, 27/01/2015 > > event3, 05/02/2015 > > > > and so on… > > > > what I am trying to figure out is, how to calculate the days between each > > event. > > > > so event2, would be 2 days, event3 would be 9 days etc. > > > > want to end up with something like this that is passed to the template > > event1, 25/01/2015 > > event2, 27/01/2015, 2 > > event3, 05/02/2015, 9 > > > > > > its the mechanics I am having trouble working out, my initial though was > to > > have a def on the model that accepted a datetime object. Somehow in a > loop i > > would get the diatomite from the next event and pass it in and calc days > on > > self.event_date… cant think how to do it. > > > > has anyone else needed something like this? or am I thinking about it > > wrong.. > > > > Thanks > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to django-users+unsubscr...@googlegroups.com. > > To post to this group, send email to django-users@googlegroups.com. > > Visit this group at http://groups.google.com/group/django-users. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/django-users/63b0691e-34b1-4634-bf29-640583f22bae%40googlegroups.com > . > > > > For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAGBeqiOF4-or%3DWY4DGbsQY1eFUY0bKR09sTVED9dEUc6tNUY9Q%40mail.gmail.com > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei1g-gMx6cqvQ9in8vq%2BkhLd9d6ccHefWKxba-vwpeW9KA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
Re: models, peeking to next record - maybe
My initial suggestion would be to utilise the "order by" option, and order by date. Then, on each model have a find_next function and a until_next function. The first would work out which was the next event, the other the time. They could be coupled, but keeping them separate means you can utilise them later if you need to. cheers L. -- "This is a profound psychological violence here. How can one even begin to speak of dignity in labor when one secretly feels one's job should not exist?" On the Phenomenon of Bullshit Jobs, David Graeber http://strikemag.org/bullshit-jobs/ On 8 February 2015 at 09:40, Richard Bowdenwrote: > Got an problem that I am struggling to solve.. > > I have: > > class Address(models.Model): > name = models.CharField() > event_date = models.DateTime() > > > event1, 25/01/2015 > event2, 27/01/2015 > event3, 05/02/2015 > > and so on… > > what I am trying to figure out is, how to calculate the days between each > event. > > so event2, would be 2 days, event3 would be 9 days etc. > > want to end up with something like this that is passed to the template > event1, 25/01/2015 > event2, 27/01/2015, 2 > event3, 05/02/2015, 9 > > > its the mechanics I am having trouble working out, my initial though was to > have a def on the model that accepted a datetime object. Somehow in a loop i > would get the diatomite from the next event and pass it in and calc days on > self.event_date… cant think how to do it. > > has anyone else needed something like this? or am I thinking about it > wrong.. > > Thanks > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/63b0691e-34b1-4634-bf29-640583f22bae%40googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGBeqiOF4-or%3DWY4DGbsQY1eFUY0bKR09sTVED9dEUc6tNUY9Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
models, peeking to next record - maybe
Got an problem that I am struggling to solve.. I have: class Address(models.Model): name = models.CharField() event_date = models.DateTime() event1, 25/01/2015 event2, 27/01/2015 event3, 05/02/2015 and so on… what I am trying to figure out is, how to calculate the days between each event. so event2, would be 2 days, event3 would be 9 days etc. want to end up with something like this that is passed to the template event1, 25/01/2015 event2, 27/01/2015, 2 event3, 05/02/2015, 9 its the mechanics I am having trouble working out, my initial though was to have a def on the model that accepted a datetime object. Somehow in a loop i would get the diatomite from the next event and pass it in and calc days on self.event_date… cant think how to do it. has anyone else needed something like this? or am I thinking about it wrong.. Thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/63b0691e-34b1-4634-bf29-640583f22bae%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
models, peeking to next record - maybe
I am working on a problem that I am trying to figure out, not having much luck so far... I have a model, basic for this example: class Event(models.Model): name = models.Charfield(max_length=10) event_date = models.DateTime() I am trying to workout how I can calculate the number of days between each event, the query set would be orders by event_date event1, 26/01/2015 event2, 29/01/2015 event3, 03/02/2015 so event 2 would be 3 days and event 3 would be 5 days and so on. my idea was to have a model def that accepted a datetime from the previous record which would calc the days against the self.event_date its the mechanics of peeking at the next / previous record in a queryset...i am struggling with, not sure how to proceed with this problem has anyone seen this before, or am I thinking about wrong ? Thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e70aaa9a-cc2d-41a3-9333-e82618a5a50f%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.