Class Based Views has ability to include function in view from template.
Short example:
I have finished small (GTS) project related to GPS system. Each car has a array of points (car positions) and route (named as LineString in GeoDjango).
part of models.py
class Points(models.Model):
""" """
Car = models.ForeignKey(Car)
Speed = models.IntegerField(default=0)
Time = models.DateTimeField()
Course = models.IntegerField(default=0)
Altitude = models.IntegerField(default=0)
Sat = models.IntegerField(default=1)
Point = gismodels.PointField()
Points has connection to the Car and has DateTime field used for correct point sorting.
HTML page (Car) should contains array of related object based on Car ID and specific time range.
I add to views.py (CarDetail class )next function:
def points(self):
""" """
timefmt= "%Y-%m-%d %H:%M:%S"
usertimezone= self.request.user.profile.timezone
try:
StartDateTime = usertimezone.localize(datetime.strptime(self.request.GET['StartDateTime'], timefmt))
EndDateTime = usertimezone.localize(datetime.strptime(self.request.GET['EndDateTime'], timefmt))
except:
StartDateTime = datetime.combine(date.today(), time.min)
EndDateTime = datetime.now()
context = super(CarDayReport, self).get_context_data()
return Points.objects.filter(Car_id=context['car'].id,
Time__gt=StartDateTime,
Time__lte=EndDateTime).order_by('Time')
Time range is set via html template. Car ID is also known. Not hard way to do.,
Many thanks,
Serge
+380 636150445
skype: skhohlov
Serge
+380 636150445
skype: skhohlov
On Sat, Feb 6, 2016 at 9:29 AM, Luca Moiana <luca.moiana@gmail.com> wrote:
Hi Serge,--
sorry for the expert warning you gave me on performance. But I have trouble following your really techincal suggestion, my goal is to have one table with measures that is related to three models with different geometries, so I can't use a foreign key in measure table, I don't understand the use of related obj, can you point me to a good source ?
thanks
On Friday, February 5, 2016 at 7:40:53 PM UTC+1, Sergiy Khohlov wrote:I've decided to use connection via key. and using via key directly or via related objects. Such us Car -> Point (multiline). Reason is simple : django creates not perfect database structure and I would like think about performance as soon as possible. My project contains Car and points related to the car and of course route (POLYLINE). I deceided to use trivial PostGIS model for avoiding bottleneck that use simple code in view. It is really easy to change view and hard to change models in case of important data present. (Every 100km generates 2000 points which cause and route length and motor service interval). In case of success Abstract class (with good performance) let me know please.On Fri, Feb 5, 2016 at 7:25 PM, Luca Moiana <luca....@gmail.com> wrote:To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/70b475b9-34d7-498c-ad1f-d27f020d02d0%40googlegroups.com.Hi Serge,--
thank you for your reply.
I'm working on an environmental monitoring app, where I want to store, and serve, monitoring value on different geometries, points, tracks or polygon.
That's why I'm trying to use a polymorphic model in order to have one entity and multiple geometries.
Cheers
L
On Friday, February 5, 2016 at 10:51:31 AM UTC+1, Sergiy Khohlov wrote:I would like as simple question : Are you planning to have some advantages using this abstract class ?I'm working on similar product (look like you are making energy pipeline system based on postgis and gjango). My product is GTS system which includes POINTS (datas are received via GTS devices mounted on vehicles). Polylines (car route for selected time range), etc. Producing abstract class can decrease DB productivity. Which one functional do you need in case of abstract class for different types usage ?On Fri, Feb 5, 2016 at 11:07 AM, Luca Moiana <luca....@gmail.com> wrote:Hi Collin,Sorry for the late reply, but I still don't get the google groups;Yes, I am tryng to crete a models where you can choose between the three geometry types.I'll go back to my app and test the code you suggested and post again the error.thanks
On Wednesday, October 28, 2015 at 7:00:26 PM UTC+1, Collin Anderson wrote:Hello,Are you trying to combine multiple models into one, like this?
class PolyModel(pdmpunto, pdmtransetto, pdmarea):
passYou could also try asking on the geodjango list: http://groups.google.com/group/geodjangoCollin
On Monday, October 26, 2015 at 7:18:24 AM UTC-4, Luca Moiana wrote:Hi, working on my first django app, and run into a problem.
I tend to create geodjango objects, and add all data from external tables with pk.
Then I want to have different geometries 8points, lines, polygons) into a unique polymorphic class, can I do that?
I have an error that I'll document later, and I'm trying to figure out what to do.
Here is the model:
import datetime
from django.db import modelsfrom django.contrib.gis.db import models as geomodelsfrom django.utils import timezonefrom django.forms import ModelFormfrom polymorphic import PolymorphicModel# Geometria linea da monitorareclass geolinea(geomodels.Model):progetto = models.CharField(max_length=14, primary_key=True)geom = geomodels.MultiLineStringField()objects = geomodels.GeoManager()def __str__(self):return '%s' % (self.progetto)# Oggetto Progetto soggetto a PMAclass linea(models.Model):progetto = models.ForeignKey(geolinea)nome = models.CharField(max_length=200)TENSIONE = (('132', '132kV'),('150', '150kV'),('220', '220kV'),('380', '380kV'),)tensione = models.CharField(max_length=5,choices=TENSIONE)def __str__(self):return '%s' % (self.nome)# Geometria dei pdmclass pdmpunto(geomodels.Model):linea = models.ForeignKey(linea)numero = models.CharField(max_length=3)geom = geomodels.PointField()objects = geomodels.GeoManager()class pdmtransetto(geomodels.Model):linea = models.ForeignKey(linea)numero = models.CharField(max_length=3)geom = geomodels.LineStringField()objects = geomodels.GeoManager()class pdmarea(geomodels.Model):linea = models.ForeignKey(linea)numero = models.CharField(max_length=3)geom = geomodels.PolygonField()objects = geomodels.GeoManager()class pdm(PolymorphicModel):numero = models.CharField(max_length=14, primary_key=True)class punto(pdm):rifpunto = models.ForeignKey(pdmpunto)class transetto(pdm):riftransetto = models.ForeignKey(pdmtransetto)class area(pdm):rifarea = models.ForeignKey(pdmarea)
Visit this group at https://groups.google.com/group/django-users.To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cd243d25-0aff-446d-b9c9-cb5682d9bdf3%40googlegroups.com.
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9da11934-7a41-40b3-be7c-d6bbb876a29c%40googlegroups.com.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADTRxJNN5SBhD-r3%3DEJ8YmxN78GDS7QVh71WtUTYuM_9mVLG7g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment