Package: django-vectortiles
Generate MapBox VectorTiles from GeoDjango modelsDirectly with PostgreSQL/PostGIS 2.4+ or python native mapbox_vector_tileRead full documentationInstallationBasicpipinstalldjango-vectortilesWithout any other option, use only vectortiles.postgisEnsure you have psycopg2 set and installedIf you don't want to use Postgispipinstalldjango-vectortiles[mapbox]This will incude mapbox_vector_tiles package and its dependenciesUse only vectortiles.mapboxExamplesassuming you have django.contrib.gis in your INSTALLED_APPS and a gis compatible database backend# in your app models.pyfromdjango.contrib.gis.dbimportmodelsclassLayer(models.Model):name=models.CharField(max_length=250)classFeature(models.Model):geom=models.GeometryField(srid=4326)name=models.CharField(max_length=250)layer=models.ForeignKey(Layer,on_delete=models.CASCADE,related_name='features')Simple model:# in your view filefromdjango.views.genericimportListViewfromvectortiles.postgis.viewsimportMVTViewfromyourapp.modelsimportFeatureclassFeatureTileView(MVTView,ListView):model=Featurevector_tile_layer_name="features"vector_tile_fields=('other_field_to_include',)# in your urls filefromdjango.urlsimportpathfromyourappimportviewsurlpatterns=[...path('tiles/<int:z>/<int:x>/<int:y>',views.FeatureTileView.as_view(),name="feature-tile"),...]Related model:# in your view filefromdjango.views.genericimportDetailViewfromvectortiles.mixinsimportBaseVectorTileViewfromvectortiles.postgis.viewsimportMVTViewfromyourapp.modelsimportLayerclassLayerTileView(MVTView,DetailView):model=Layervector_tile_fields=('other_field_to_include',)defget_vector_tile_layer_name(self):returnself.get_object().namedefget_vector_tile_queryset(self):returnself.get_object().features.all()defget(self,request,*args,**kwargs):self.object=self.get_object()returnBaseVectorTileView.get(self,request=request,z=kwargs.get('z'),x=kwargs.get('x'),y=kwargs.get('y'))# in your urls filefromdjango.urlsimportpathfromyourappimportviewsurlpatterns=[...path('layer/<int:pk>/tile/<int:z>/<int:x>/<int:y>',views.LayerTileView.as_view(),name="layer-tile"),...]Usage without PostgreSQL / PostGISJust import and use vectortiles.mapbox.view.MVTView instead of vectortiles.postgis.view.MVTViewUsage with DRFdjango-vectortiles can be used with DRF ifrenderer_classesof the view is overridden (seeDRF docs). Simply use the right BaseMixin and action on viewsets, or directly a GET method in an APIView, i.e.:fromrest_frameworkimportrenderers,viewsfromvectortiles.postgis.viewsimportMVTViewclassMVTRenderer(renderers.BaseRenderer):media_type="application/vnd.mapbox-vector-tile"format="pbf"defrender(self,data,accepted_media_type=None,renderer_context=None):returndataclassTileServerView(MVTView,views.APIView):renderer_classes=[MVTRenderer]defget(...):...DevelopmentWith docker and docker-composedockerpullmakinacorpus/geodjango:bionic-3.6
docker-composebuild# docker-compose updocker-composerun/code/venv/bin/python./manage.pytestLocalInstall python and django requirements (python 3.6+, django 2.2+)Install geodjango requirementsHave a postgresql / postgis 2.4+ enabled databaseUse a virtualenvpipinstall.[dev]-UCHANGELOG0.2.0       (2022-10-17)Possibly breaking change:Base Mixin method get_tile use now class attributes for extent / buffer or clip_geom. Remove this parameters in your sub class method if needed.Bug fixes:Correct usage for vector_tile_extent / vector_tile_buffer and vector_tile_clip_geomClipped geom is now working for mapboxSupport Python 3.10 and django 4.10.1.0       (2021-02-25)First beta releaseAdd attribute to limit features in tile (unable to use a sliced queryset)0.0.3       (2021-02-18)Delete useless Envelope transformation because django implicitly transform on intersects lookup (thanks to StefanBrand)Avoid useless queryset evaluation in some cases (thanks to StefanBrand)0.0.2       (2021-02-12)Fix required 'fields' key in tilejson. Will be filled laterFix generated subquery to deal with DateField (thanks to StefanBrand)0.0.1       (2020-10-22)First ReleaseGenerate Vector Tiles from django modelsin pythonwith PostGISGenerate associated TileJSONDefault views to handle Vector tiles and tilejson