django instances updates and recall values from history -
i can track model instances updates history. not know how proceed further can recall instances values specific date / time.
class room
used create new rooms. class roomlog
used track changes. if update room instance related roomlog instance created. effect in creating roomlog instances every room instance update.
so, can change specific instance few times within e.g. minute or hour. can same other instances in same time or example can not change instance or change of other instances , not change of them.
although,
i recall whole table latest values of instances specific hour /day or within specific period e.g.
if choose time 1:00am table rendered instances created @ time. let's call table v0.
now start updates , instances changed between 1:00am & 2:00am. so, re-call table displayes instances time 2:00am , keep in time instances changed not. so, of them have history of changes , not have.
and same other hours / days. hope sense of want achieve.
how ?
these 2 models use operate on room , related roomlog instances.
class room(models.model): room_name = models.charfield(max_length= 10) room_value = models.positivesmallintegerfield(default=0) flat = models.foreignkey(flat) created_date = models.datefield(auto_now_add= true) created_time = models.timefield(auto_now_add= true) def __init__(self, *args, **kwargs): super(room, self).__init__(*args, **kwargs) self.value_original = self.room_value def save(self, **kwargs): transaction.atomic(): response = super(room, self).save(**kwargs) if self.value_original != self.room_value: room_log = roomlog() room_log.room = self room_log.room_value = self.value_original room_log.save() return response class meta: ordering = ('room_name',) def __unicode__(self): return self.room_name class roomlog(models.model): room = models.foreignkey(room) room_value = models.positivesmallintegerfield(default=0) update_date = models.datefield(auto_now_add= true) update_time = models.timefield(auto_now_add= true) def __str__(self): return '%s | %s | %s' % (self.room, self.update_date, self.update_time)
edit
answer below djq gt & lte pointed me towards solution. how have solved in views:
class allroomsview(listview): template_name = 'prostats/roomsdetail.html' queryset = room.objects.all() def get_context_data(self, **kwargs): context = super(allroomsview, self).get_context_data(**kwargs) timenow = tz.now() timeperiod= timedelta(hours=1) deltastart = timenow - timeperiod context['rooms'] = room.objects.all() context['rlog'] = roomlog.objects.all() context['rfiltered'] = roomlog.objects.filter(update_time__gt = deltastart) context['rfilteredcount'] = roomlog.objects.filter(update_time__gt = deltastart).count() print timenow choosestart = '22:04:30.223113' choosend = '22:54:30.223113' context['roomfiltertest'] = roomlog.objects.filter(update_time__gt = choosestart, update_time__lte = choosend) return context
you should able filter model time ranges. might easier datetimefield
. here's example of how filter time range (assuming datetimefield
, , different model structure fields booking_started
, booking_ended
)
from datetime import timedelta django.utils import timezone tz start = tz.now() time_range = timedelta(hours=2) end = start - time_range room.objects.filter(booking_started__lte=start, booking_ending__gt=end)
lte
, gt
shorthands less or equal to
or greater than
Comments
Post a Comment