Opened 20 months ago

Last modified 14 months ago

#30049 assigned Bug

GIS widgets don't initialize properly after clicking "Add another" in admin inlines

Reported by: Lars Solberg Owned by: Pavel Tvrdík
Component: GIS Version: master
Severity: Normal Keywords: gis, admin
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Lars Solberg)

My goal is to use a field that looks like this:

from django.contrib.gis.db import models

class FavoriteLocation(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    location = models.PointField()

as an extra field on a User. The field-type works on other models, so everything is setup correctly.

However, getting it as a inline is problematic because it doesnt render correctly in the admin.
The admin code looks like this:

from django.contrib.auth.admin import UserAdmin
from django.contrib.gis import admin

class FavoriteLocationInline(admin.StackedInline):
    model = FavoriteLocation
    extra = 1

class CustomUserAdmin(UserAdmin):
    inlines = (FavoriteLocationInline,), CustomUserAdmin)

however, the widget for displaying the field looks blank (see screenshot).

In the doc, It sais I should use "admin.OSMGeoAdmin" for my admin class. However, that don't change anything.
Nor changing it to "class CustomUserAdmin(admin.OSMGeoAdmin, UserAdmin).

If I click add "another favorite location", I will also get a text-area field instead of the widget.

Trying to save the model with this broken widget gives me a "GDALException" (django.contrib.gis.gdal.error.GDALException: OGR failure.)
Note that this widget works on other admin-pages I have..

Also note that the broken widget is black because it is all zoomed in on 0, 0.. However, it also misses some elements in the upper right corner.
I am seeing no errors in the browser console.

Attachments (3)

Screenshot 2018-12-20 at 06.13.12.png (38.1 KB) - added by Lars Solberg 20 months ago.
broken widget
Screenshot 2018-12-20 at 06.26.39.png (158.9 KB) - added by Lars Solberg 20 months ago.
top of working widget (2.3 KB) - added by Tim Graham 20 months ago.
sample app to reproduce

Download all attachments as: .zip

Change History (11)

Changed 20 months ago by Lars Solberg

broken widget

comment:1 Changed 20 months ago by Lars Solberg

Description: modified (diff)

info about broken widget

Changed 20 months ago by Lars Solberg

top of working widget

comment:2 Changed 20 months ago by Tim Graham

I think you should try inheriting from OSMGeoAdmin rather than UserAdmin. You'll have to copy the parts of UserAdmin that you want into your subclass, I imagine. I imagine this use case isn't too common and I'm not sure if there's anything Django could do to make it easier.

This looks more like a usage question than a bug in Django, but if you have some idea about what could be done, feel free to let us know.

comment:3 Changed 20 months ago by Lars Solberg

I still coulnt get it to work.

I tried using one I have working as an inline, then the inlined version stopped working as well.
So this happens when I am using inlines..

The html to render the widgets are wastly different..

In this case, I have taken out the useradmin part, and have made a normal admin interface..

from django.contrib.gis import admin
from .models import Other, Spot

class SpotInline(admin.StackedInline):
    model = Spot
    extra = 1

class OtherAdmin(admin.OSMGeoAdmin):
    inlines = (SpotInline,), OtherAdmin)

OtherAdmin now has the map as a normal widget (works), and one as an inline (doesnt work).

I can see in eg that there are some references around to example {{ id }}_map.. Will id be correct when this is using an inline?

comment:4 Changed 20 months ago by Tim Graham

It's possible that inline GIS widgets are broken. I'm not sure if the Django tests cover that. Can you put together a small sample project that reproduces the issue? You could also check with if it works with older versions (2.0.x, 1.11.x, etc.) of Django.

comment:5 Changed 20 months ago by Tim Graham

Summary: Multiple problems using gis fields in admin inlinesGIS widgets don't initialize properly after clicking "Add another" in admin inlines
Triage Stage: UnreviewedAccepted

Using the attached sample project, I can reproduce the problem that clicking "Add another City" on the admin's state change form doesn't initialize the new widget properly (textarea instead of the openlayers widget). It doesn't seem to be a new issue -- I reproduced back as far as I tested (Django 1.11).

I'm not sure if the "missing elements in the upper right corner" part of this report is valid... when using PointField in a non-inline admin, those elements don't appear either. Are they not applicable to PointField? In any case, a bug report should be limited to one issue so let's keep this ticket limited to the "Add another" initialization and open addition tickets if more issues remain.

Changed 20 months ago by Tim Graham

Attachment: added

sample app to reproduce

comment:6 Changed 15 months ago by Pavel Tvrdík

Owner: changed from nobody to Pavel Tvrdík
Status: newassigned

comment:7 Changed 14 months ago by felixxm

Has patch: set
Version: 2.1master

comment:8 Changed 14 months ago by felixxm

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top