class StoreForm(forms.ModelForm): location = fields.GeometryField(widget=forms.HiddenInput()) def __init__(self, *args, **kwargs): current_ip = kwargs.pop('current_ip', None) super(StoreForm, self).__init__(*args, **kwargs) # Make sure that we store the initial data as GeoJSON so that # it is easier for us to use it in Javascript. instance = kwargs.get('instance', None) if instance: self.initial['location'] = instance.location.geojson else: try: self.initial['location'] = GeoIP().geos(current_ip).geojson except AttributeError: pass class Meta: model = get_model('stores', 'Store') exclude = ('slug', ) widgets = { 'description': forms.Textarea(attrs={ 'cols': 40, 'rows': 10 }), }
class StoreForm(forms.ModelForm): location = fields.GeometryField(widget=forms.HiddenInput()) class Meta: model = get_model('stores', 'Store') fields = [ 'name', 'manager_name', 'phone', 'email', 'reference', 'image', 'description', 'location', 'group', 'is_pickup_store', 'is_active', ] widgets = { 'description': forms.Textarea(attrs={ 'cols': 40, 'rows': 10 }), } def __init__(self, *args, **kwargs): current_ip = kwargs.pop('current_ip', None) super(StoreForm, self).__init__(*args, **kwargs) # Make sure that we store the initial data as GeoJSON so that # it is easier for us to use it in Javascript. instance = kwargs.get('instance', None) if instance: self.initial['location'] = instance.location.geojson elif HAS_GEOIP2 and getattr(settings, 'GEOIP_ENABLED', True): from django.contrib.gis.geoip2 import GeoIP2 point = GeoIP2().geos(current_ip) if point: self.initial['location'] = point.geojson def clean_reference(self): ref = self.cleaned_data['reference'] if ref == "": return None return ref
class StoreForm(forms.ModelForm): location = fields.GeometryField(widget=forms.HiddenInput()) class Meta: model = get_model('stores', 'Store') fields = [ 'name', 'manager_name', 'phone', 'email', 'reference', 'image', 'description', 'location', 'group', 'is_pickup_store', 'is_active', ] widgets = { 'description': forms.Textarea(attrs={ 'cols': 40, 'rows': 10 }), } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # Make sure that we store the initial data as GeoJSON so that # it is easier for us to use it in Javascript. instance = kwargs.get('instance', None) if instance: self.initial['location'] = instance.location.geojson def clean_reference(self): ref = self.cleaned_data['reference'] if ref == "": return None return ref