django-leaflet
django-leaflet copied to clipboard
PointField in form on save give TypeError: <Point object at 0x7fbf5c0f21c0> is not JSON serializable
model.py
`class GeorefencedImage(models.Model):
geom = PointField()
comment = models.TextField()
image = ImageField(processors=[Transpose(),ResizeToFit(600, 600)],
format='jpeg',
options={'quality': 70})
view.py
class ImageForm(forms.ModelForm):
geom = PointField()
class Meta:
model = GeorefencedImage
fields = ( 'geom','image','comment')
def insertData(request):
if request.method == 'POST': # If the form has been submitted...
form = ImageForm(request.POST, request.FILES) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
form.save()
else:
form = ImageForm() # An unbound form
return render(request, 'insertdata/form.html',{'form': form})
settings.py
SERIALIZATION_MODULES = {
'geojson' : 'djgeojson.serializers'
}
url.py
urlpatterns = [
url(r'^$',
insertData,name="insertdata"),
]
I get:
Django version 1.9, using settings 'rmap.settings'
Starting development server at http://127.0.0.1:8888/
Quit the server with CONTROL-C.
[25/Feb/2016 21:02:11] "GET /insertdata/ HTTP/1.1" 200 5975
Internal Server Error: /insertdata/
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/pat1/git/rmap/python/insertdata/views.py", line 43, in insertData
form.save()
File "/usr/lib/python2.7/site-packages/django/forms/models.py", line 451, in save
self.instance.save()
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 700, in save
force_update=force_update, update_fields=update_fields)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 728, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 812, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 851, in _do_insert
using=using, raw=raw)
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
for sql, params in self.as_sql():
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1023, in as_sql
for obj in self.query.objs
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 962, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
File "/usr/lib/python2.7/site-packages/jsonfield/fields.py", line 100, in get_db_prep_value
return json.dumps(value, **self.dump_kwargs)
File "/usr/lib64/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/site-packages/jsonfield/encoder.py", line 59, in default
return super(JSONEncoder, self).default(obj)
File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Point object at 0x7fbf5c0f21c0> is not JSON serializable
[25/Feb/2016 21:02:28] "POST /insertdata/ HTTP/1.1" 500 160979
Thanks for your attention Paolo
It works with:
class ImageForm(forms.ModelForm):
#geom = PointField()
class Meta:
model = GeorefencedImage
fields = ('geom','image','comment')
widgets = {'geom': LeafletWidget()}
as suggested for new django versions, but the widget show a "full" menu, not only the geopoint marker.
The problem there is with forms.Form too, so is not possible to use simple forms too.
https://github.com/Ry10p/django-leaflet/blob/master/README.rst
I ran into the same problem so I did a 3 or 4 line change in the forms(js) to make it work
IM NEW PLEASE DONT JUDGE TO HARD
As for Django 2.0.2, using PointField() still throws an error but widgets = {'geom': LeafletWidget()} works.