D3sa
D3sa

Reputation: 241

Django views for statistics

I am currently facing a problem with creating a statistical overview of my app. The app contains a geodjango multipoligon model (Area) that other models (Hotels,Cafes,Museums etc.) reference for their location:

class Area(models.Model):
    area_name = models.CharField(max_length=100,  null=True,  blank=True)
    mpoly = models.MultiPolygonField(srid=4326,  null=True,  blank=True)

class Hotel/Restaurant/Museum/Traffic_Incident(models.Model):
    area = models.ForeignKey(Area)

Is it possible to create a view in django where I can show all kinds of places within the area (Area model)? Due to specific reasons I do not want to mix the other models together. What I am looking for is a way to create a map with all the objects on the map as well as statistics on what types of establishments are in the area. I have no trouble with creating a map and passing the geojson data to django-leaflet, but I am struggling to create a table with statistics regarding the actual places in the area.

I can make something like this by creating a model for the statistics, but I am reluctant to do that, because the data would be dynamic and I do not want to create a simple counter for statistics, although I might have to do just that if there are no other options.

Upvotes: 2

Views: 378

Answers (1)

D3sa
D3sa

Reputation: 241

I ended up creating a custom form:

class MyCustomForm(forms.Form):
restaurants = forms.IntegerField(label="Restaurants in the area")

and then assigning values to the form via views.py

class AreaView(MultiFormsView):
    template_name = 'area/area_view.html'
    form_classes = {'area':MyCustomForm,
## some other logic here,
}   
    def get_success_url(self):
        ##some logic

    def get_area_form_initial(self):
        area = get_object_or_404(Area, pk=self.kwargs['pk'])
        restaurants = Restaurants.objects.filter(area=area.pk).count()
## somelogic
        return {'restaurants': restaurants, 
}

    def get_context_data(self, **kwargs):
        context = super(AreaView, self).get_context_data(**kwargs)
        return context

Upvotes: 2

Related Questions