From 1ecd55dece20f72ff933de05dc5a778bcf3be91c Mon Sep 17 00:00:00 2001 From: simon-the-shark Date: Fri, 4 Dec 2020 20:26:38 +0100 Subject: [PATCH] fixed admin customization --- README.md | 27 +------------------------- mapbox_location_field/forms.py | 7 ++++--- mapbox_location_field/spatial/forms.py | 4 ++-- setup.py | 4 ++-- 4 files changed, 9 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index d60fd1c..155e284 100644 --- a/README.md +++ b/README.md @@ -147,32 +147,7 @@ from mapbox_location_field.spatial.admin import SpatialMapAdmin admin.site.register(SomeLocationModel, SpatialMapAdmin) ``` * #### Admin customization: -Unfortunetly I haven't found any solution that pass [map_attrs](#map_attrs) automatically from model definition. -The only working way of [customization](#customization) that works in admin panel is overriding the modelform. - -```python -from django.contrib import admin -from django.forms import ModelForm -from mapbox_location_field.admin import MapAdmin -from mapbox_location_field.forms import LocationField - -from .models import SomeLocationModel - - -class PlaceForm(ModelForm): - class Meta: - model = Place fields = "__all__" - location = LocationField(map_attrs={"style": "mapbox://styles/mapbox/satellite-v9"}) - - -class MyMapAdmin(MapAdmin): - form = PlaceForm - -admin.site.register(SomeLocationModel, MyMapAdmin) -``` -For spatial field replace the `mapbox_location_field.admin.MapAdmin` with the `mapbox_location_field.spatial.admin.SpatialMapAdmin` and the `mapbox_location_field.forms.LocationField` with `mapbox_location_field.spatial.forms.LocationField` - -In examples above, `SomeLocationModel` is name of your model, like in [usage section](#usage). +Since `v1.7.0` admin customization should work out of the box, automatically using the `map_attrs` from model definition. # AddressAutoHiddenField AddressAutoHiddenField is field for storing address. It uses AddressAutoHiddenInput which is hidden and when you place your marker on map, automatically fill itself with proper address. diff --git a/mapbox_location_field/forms.py b/mapbox_location_field/forms.py index 98e334c..6bc2ceb 100644 --- a/mapbox_location_field/forms.py +++ b/mapbox_location_field/forms.py @@ -44,9 +44,9 @@ class LocationField(forms.CharField): def __init__(self, *args, **kwargs): map_attrs = kwargs.pop("map_attrs", None) - self.widget = MapInput(map_attrs=map_attrs, ) - super().__init__(*args, **kwargs) + + self.widget = MapInput(map_attrs=map_attrs, ) self.error_messages = {"required": "Please pick a location, it's required", } def to_python(self, value): @@ -59,6 +59,7 @@ class AddressAutoHiddenField(forms.CharField): def __init__(self, *args, **kwargs): map_id = kwargs.pop("map_id", "map") - self.widget = AddressAutoHiddenInput(map_id=map_id) super().__init__(*args, **kwargs) + + self.widget = AddressAutoHiddenInput(map_id=map_id) self.label = "" diff --git a/mapbox_location_field/spatial/forms.py b/mapbox_location_field/spatial/forms.py index 887d9a0..3b7ae25 100644 --- a/mapbox_location_field/spatial/forms.py +++ b/mapbox_location_field/spatial/forms.py @@ -10,9 +10,9 @@ class SpatialLocationField(PointField): def __init__(self, *args, **kwargs): map_attrs = kwargs.pop("map_attrs", None) - self.widget = MapInput(map_attrs=map_attrs, ) - super().__init__(*args, **kwargs) + + self.widget = MapInput(map_attrs=map_attrs, ) self.error_messages = {"required": "Please pick a location, it's required", } def clean(self, value): diff --git a/setup.py b/setup.py index adeeb1d..eb61eb3 100644 --- a/setup.py +++ b/setup.py @@ -10,14 +10,14 @@ setup( name='django-mapbox-location-field', - version='1.6.4', + version='1.7.0', packages=["mapbox_location_field"], include_package_data=True, license='MIT License', description='location field with MapInput widget for picking some location', long_description=README, long_description_content_type="text/markdown", - download_url="https://github.com/Simon-the-Shark/django-mapbox-location-field/archive/v1.6.4.tar.gz", + download_url="https://github.com/Simon-the-Shark/django-mapbox-location-field/archive/v1.7.0.tar.gz", url='https://github.com/Simon-the-Shark/django-mapbox-location-field', author='Szymon KowaliƄski', author_email='simonrekin@gmail.com',