Skip to content

Commit

Permalink
fixed admin customization
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-the-shark committed Dec 4, 2020
1 parent 9d519cc commit 1ecd55d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 33 deletions.
27 changes: 1 addition & 26 deletions README.md
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions mapbox_location_field/forms.py
Expand Up @@ -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):
Expand All @@ -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 = ""
4 changes: 2 additions & 2 deletions mapbox_location_field/spatial/forms.py
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -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',
Expand Down

0 comments on commit 1ecd55d

Please sign in to comment.