0

虽然这可能是初级的,但对我来说它证明超出了我的水平。我先谢谢了。

我想要实现的是我正在查询的一行数据,在根据 Liveroute 表中的字段值不活动进行查询后,我执行计算查找两组经度和纬度值之间的距离,我可以使用haversine公式来计算两点经度和纬度之间的距离。但我担心这个计算过程可能需要时间,而且我将无法按时显示表格中大量行的数据。所以我想我会将计算结果保存在另一个表中并从该表中获取数据并显示它。

我将对表中变为非活动的值执行计算。这是 Django 代码,它告诉 Liveroute 模型类中的路由该行是活动的还是非活动的。

class LiveRoutes(models.Model):
   user = models.ForeignKey(User)
   route = models.ForeignKey(UserRoutes)
   status = models.ForeignKey(LiveRoutesStatus)
   traveller = models.ManyToManyField(LiveRouteTravellers)
   datetime = models.DateTimeField()

 def __unicode__(self):
    return self.route.__unicode__()

 def isActive(self):
   utc = pytz.utc
   os.environ['TZ'] = 'UTC'
   local = pytz.timezone("Asia/Calcutta")
   now = utc.localize(datetime.datetime.today())
   now = now.astimezone(local)
   time_delta = (local.localize(self.datetime.replace(tzinfo=None)) +    datetime.timedelta(minutes=self.route.journey_time_day)) - now
   if time_delta.days == -1 and (24 - (time_delta.seconds / 3600)) <= 2:
     return True
   elif time_delta.days >= 0:
     return True
   else:
     return False

基于 isActive 函数的这个值,我想执行如下计算

def carbonFootPrint(request):
if request.method != "GET":
   raise Http404

routes = LiveRoutes.objects.all();
routeDetailArr = []
for lroute in routes:
   routeDetail = dict()
   if lroute.isActive() == False:

      #Now I need to find out the start location and end location for the journey and the number of travellers. 
      routeDetail['travellers'] = lroute.traveller.all().count()
      routeDetail['start_loc_lat']= lroute.route.start_location.latitude
      routeDetail['start_loc_long'] = lroute.route.start_location.longitude
      routeDetail ['end_loc_lat'] = lroute.route.end_location.latitude
      routeDetail['end_loc_long'] =  lroute.route.end_location.longitude
      routeDetail['distance'] = haversine(start_loc_lat,start_loc_long,end_loc_lat,end_loc_long)
      routeDetailArr.append(routeDetail)

我的问题是如何将所有这些数据插入到另一个表中,以便稍后我可以获取这些值。感谢任何建议将不胜感激。

4

1 回答 1

1

为了触发会话,它必须是这样的:

request.session['travellers'] = lroute.traveller.all().count()
//other session here

要获取和使用数据,它必须是:

travellers = request.session.get('travellers')
于 2013-02-25T09:26:23.127 回答