0

I have a Django app, where each user can add a product with multiple possible metrics (width, height and length combination). A user must also specify in which city this product is located. Users can also search within the database all products matching specific metrics. I use Django 1.11 and am seaching for a solution to display on an interactive map all the products matching a queryset.

I am trying to do it with django-leaflet and django-geojson (as my db is not gis-oriented and I don't need heavy geo-computations), but I am facing some difficulties because my "PointField" is not in my product Model but in the Location Model and on the map I need to display Product properties, so I must serialize all these data together.

If you prefer code rather than words, here is a simplified version of my relevant files.

#models.py

class Product(models.Model):
    name = models.CharField()
    owner = models.ForeignKey(User)
    photo = models.ImageField(...)
    dimensions = models.ManyToManyField(Metrics)
    location = models.ForeignKey(Location, related_name='products', related_query_name='product')

class Metrics(models.Model):
   width = models.PositiveIntegerField()
   height = models.PositiveIntegerField()
   length = models.PositiveIntegerField()

class Location(models.Model):
   zip_code = models.PositiveIntegerField()
   city_name = models.CharField()
   slug = models.SlugField(max_length=500, blank=True)
   geom = PointField(default={'type': 'Point', 'coordinates': [0, 0]})


#views.py

class SearchResultListView(ListView):
     model = models.Product
     template_name='my_app/searchresult_list.html'
     context_object_name = 'product_list'

     def get_context_data(self, **kwargs):
         context = super().get_context_data(**kwargs)
         query_width = self.request.GET['width']
         query_height = self.request.GET['height']
         query_length = self.request.GET['length']
         context['product_list'] = context['product_list'].filter(metrics__width=query_width, 
         metrics__length=query_length, metrics__height=query_height)
         return context

#urls.py

????

#template.html

????

I saw in the django-geojson documentation multiple ways to hit the db (GeoJSON layer view, Tiled GeoJSON layer view, GeoJSON template filter, low-level serialization). But I struggle to find the way to match my needs as my properties are in the Product Model, my coordinates are in the Location Model and my queryset in a non-related class-based view.

Any idea on the best way to perform my task? Should I continue with django-geojson or are there better apps for my purpose?

1 Answer 1

0

Your Product and Location look OK, but it is not clear what you are trying to do with Metrics. To select products near some place you want something like:

queryset = Product.objects.filter(location__geom__distance_lt=(someLocation, D(m=50)))

https://docs.djangoproject.com/en/3.0/ref/contrib/gis/geoquerysets/#distance-lt

Sign up to request clarification or add additional context in comments.

2 Comments

Metrics are just characteristics of my Product. A product can have multiple Metrics, thus there is a ManytoMany field that binds Product and Metrics. My question is about how to render on a map the location (and product details) of all the products matching the queryset as defined in my views.py. context['product_list'] = context['product_list'].filter(metrics__width=query_width, metrics__length=query_length, metrics__height=query_height)
In other words, let's consider that I have my desired product queryset: queryset = Product.objects.filter(....some filter...) I guess I will have to serialize in geojson this queryset (without using gis methods because I don't have a gis-db) to pass it to JS code in my template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.