Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to edit my Field in Admin with django-leaflet #61

Open
batisteo opened this issue Jun 13, 2015 · 8 comments
Open

Unable to edit my Field in Admin with django-leaflet #61

batisteo opened this issue Jun 13, 2015 · 8 comments

Comments

@batisteo
Copy link

Hî!
I encountered a bug using Python 3.4 and Django 1.8, attempting to use the admin.

my_app.admin.py:

from django.contrib import admin
from leaflet.admin import LeafletGeoAdmin
from .models import Place

admin.site.register(Place, LeafletGeoAdmin)

my_app.models.py:

from djgeojson.fields import PointField
from django.db import models

class Place(models.Model):
    name = models.CharField(max_length=255)
    geom = PointField()

Traceback:

Environment:

Request Method: GET
Request URL: http://localhost:8000/admin/maps/place/add/

Django Version: 1.8.2
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'leaflet',
 'djgeojson',
 'maps')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/contrib/admin/options.py" in wrapper
  616.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/contrib/admin/sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/contrib/admin/options.py" in add_view
  1516.         return self.changeform_view(request, None, form_url, extra_context)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/utils/decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/lib/python3.4/contextlib.py" in inner
  30.                 return func(*args, **kwds)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/contrib/admin/options.py" in changeform_view
  1456.         ModelForm = self.get_form(request, obj)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/contrib/admin/options.py" in get_form
  675.             fields = flatten_fieldsets(self.get_fieldsets(request, obj))
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/contrib/admin/options.py" in get_fieldsets
  354.         return [(None, {'fields': self.get_fields(request, obj)})]
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/contrib/admin/options.py" in get_fields
  664.         form = self.get_form(request, obj, fields=None)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/contrib/admin/options.py" in get_form
  700.             return modelform_factory(self.model, **defaults)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/forms/models.py" in modelform_factory
  545.     return type(form)(class_name, (form,), form_class_attrs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/forms/models.py" in __new__
  285.                                       opts.help_texts, opts.error_messages)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/forms/models.py" in fields_for_model
  216.             formfield = formfield_callback(f, **kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/leaflet/admin.py" in formfield_for_dbfield
  41.             return db_field.formfield(**kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/djgeojson/fields.py" in formfield
  65.         return super(GeoJSONField, self).formfield(**kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/jsonfield/fields.py" in formfield
  48.         return super(JSONField, self).formfield(**defaults)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/db/models/fields/__init__.py" in formfield
  912.         return form_class(**defaults)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/jsonfield/forms.py" in __init__
  15.         super(JSONFormField, self).__init__(*args, **kwargs)
File "/home/batisteo/.virtualenvs/env/lib/python3.4/site-packages/django/forms/fields.py" in __init__
  214.         super(CharField, self).__init__(*args, **kwargs)

Exception Type: TypeError at /admin/maps/place/add/
Exception Value: __init__() got an unexpected keyword argument 'geom_type'

I was able to work around this issue by commenting the formfield() method, in fields.py:63:

class GeoJSONField(JSONField):
    description = _("Geometry as GeoJSON")
    form_class = GeoJSONFormField
    dim = 2
    geom_type = 'GEOMETRY'

    # def formfield(self, **kwargs):
        # kwargs.setdefault('geom_type', self.geom_type)
        # return super(GeoJSONField, self).formfield(**kwargs)

It seems the geom_type is correctly taken in the FormField. So, what is the point of this method?

@kevcooper
Copy link
Contributor

@batisteo I received the same error due to having the wrong dependency installed. You want the package "jsonfield" and my guess is you currently have "django-jsonfield". It's confusing since PyPi has both of them named django-jsonfield, but pip uninstall django-jsonfield && pip install jsonfield should fix this

@leplatrem
Copy link
Collaborator

I also released django-geojson 1.8.1 with @kevcooper fix about GEOS detection. Let us know :)

@V1ce
Copy link

V1ce commented Jun 29, 2016

Hi,

I actually got the same error.

apps :

Django 1.9.7
python 3.4.2
djgeojson 2.9.0
django-leaflet 0.18.1
django-jsonfield 1.0.0
jsonfield 1.0.3

models.py :

class lieu(models.Model):
    nom = models.CharField(max_length = 100)
    geom = GeometryField()

admin.py :

from plan.models import lieu

admin.site.register(lieu, LeafletGeoAdmin)

traceback :

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/fr/admin/plan/lieu/add/

Django Version: 1.9.7
Python Version: 3.4.2
Installed Applications:
['djangocms_admin_style',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.admin',
 'django.contrib.sites',
 'django.contrib.sitemaps',
 'django.contrib.staticfiles',
 'django.contrib.messages',
 'cms',
 'menus',
 'sekizai',
 'treebeard',
 'djangocms_text_ckeditor',
 'djangocms_style',
 'djangocms_column',
 'filer',
 'easy_thumbnails',
 'cmsplugin_filer_image',
 'cmsplugin_filer_file',
 'cmsplugin_filer_folder',
 'cmsplugin_filer_teaser',
 'cmsplugin_filer_utils',
 'cmsplugin_filer_video',
 'djangocms_googlemap',
 'djangocms_inherit',
 'djangocms_link',
 'reversion',
 'bgl',
 'ckeditor',
 'recurrence',
 'haystack',
 'aldryn_common',
 'aldryn_boilerplates',
 'aldryn_video',
 'aldryn_search',
 'aldryn_bootstrap3',
 'standard_form',
 'debug_toolbar',
 'spurl',
 'leaflet',
 'djgeojson',
 'adminsortable2',
 'gunicorn',
 'nocaptcha_recaptcha',
'plan']
Installed Middleware:
['debug_toolbar.middleware.DebugToolbarMiddleware',
 'cms.middleware.utils.ApphookReloadMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'cms.middleware.user.CurrentUserMiddleware',
 'cms.middleware.page.CurrentPageMiddleware',
 'cms.middleware.toolbar.ToolbarMiddleware',
 'cms.middleware.language.LanguageCookieMiddleware']



Traceback:

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/contrib/admin/options.py" in wrapper
  541.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/contrib/admin/sites.py" in inner
  244.             return view(request, *args, **kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/contrib/admin/options.py" in add_view
  1437.         return self.changeform_view(request, None, form_url, extra_context)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/usr/lib/python3.4/contextlib.py" in inner
  30.                 return func(*args, **kwds)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/contrib/admin/options.py" in changeform_view
  1367.         ModelForm = self.get_form(request, obj)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/contrib/admin/options.py" in get_form
  639.             return modelform_factory(self.model, **defaults)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/forms/models.py" in modelform_factory
  545.     return type(form)(class_name, (form,), form_class_attrs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/forms/models.py" in __new__
  247.                                       opts.field_classes)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/forms/models.py" in fields_for_model
  176.             formfield = formfield_callback(f, **kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/leaflet/admin.py" in formfield_for_dbfield
  42.             return db_field.formfield(**kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/djgeojson/fields.py" in formfield
  65.         return super(GeoJSONField, self).formfield(**kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/jsonfield/fields.py" in formfield
  46.         return super(JSONField, self).formfield(**defaults)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/db/models/fields/__init__.py" in formfield
  903.         return form_class(**defaults)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/jsonfield/forms.py" in __init__
  15.         super(JSONFormField, self).__init__(*args, **kwargs)

File "/home/vincent/venv/cms/lib/python3.4/site-packages/django/forms/fields.py" in __init__
  229.         super(CharField, self).__init__(*args, **kwargs)

Exception Type: TypeError at /fr/admin/plan/lieu/add/
Exception Value: __init__() got an unexpected keyword argument 'geom_type'

I got this after upgrade django to 1.9 from 1.8, django-leaflet to 0.18 from 0.17 and django-geojson 2.9.0 from 2.8.1

I try the @kevcooper manipulation on django-jsonfield but no success because "jsonfield" don't contain a forms module, django-geojson need both apps for work.

Like you see i use django-cms but i think it is not the problem, thats work with it before the upgrade.

Thanks

@V1ce
Copy link

V1ce commented Jul 1, 2016

Fixed this with downgrade django from 1.9.7 to 1.8.13 , jsonfield 1.0.3 to 1.0.0 and django-geojson 2.9.0 to 2.8.1 and this work, and then re-upgrade all to latest and work too.... Don't understand.

@stitch
Copy link

stitch commented Mar 20, 2018

Same issue here... can't fix it by installing the other package... still need to trace the issue. Keep u posted.

@stitch
Copy link

stitch commented Mar 21, 2018

After reading http://fle.github.io/easy-webmapping-with-django-leaflet-and-django-geojson.html i assumed(!) that just adding django-geojson and django-leaflet to the requirements / installing them with pip is enough.

What i forgot is to install them properly. After reading through the manuals:
1: https://django-geojson.readthedocs.io/en/latest/installation.html
2: https://django-leaflet.readthedocs.io/en/latest/installation.html

It's mentioned that you must add them to INSTALLED_APPS too, and you indeed need jsonfield, not django-jsonfield. Obvious :)

After that's done it works like a charm.

(Do note that these fields do not store raw geojson: they store their own structure which may not be compatible with the data you're processing now. However: that leads to different errors and different challenges :))

@batisteo
Copy link
Author

I’m not using it anymore, so I can't test the original issue. You can close it at will.

@leplatrem
Copy link
Collaborator

It would be nice to propose a fix on the documentation to make it «obvious»

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants