You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
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
Lachlan Musicman
unread,
Feb 7, 2015, 8:02:23 PM2/7/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
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?"
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com, vkhe...@gmail.com
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