Opened 6 years ago

Last modified 4 months ago

#30049 assigned Bug

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

Reported by: Lars Solberg Owned by: Claude Paroz
Component: GIS Version: dev
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,)

admin.site.unregister(User)
admin.site.register(User, 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 6 years ago.
broken widget
Screenshot 2018-12-20 at 06.26.39.png (158.9 KB ) - added by Lars Solberg 6 years ago.
top of working widget
t30049.zip (2.3 KB ) - added by Tim Graham 6 years ago.
sample app to reproduce

Download all attachments as: .zip

Change History (16)

by Lars Solberg, 6 years ago

broken widget

comment:1 by Lars Solberg, 6 years ago

Description: modified (diff)

info about broken widget

by Lars Solberg, 6 years ago

top of working widget

comment:2 by Tim Graham, 6 years ago

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 by Lars Solberg, 6 years ago

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,)

admin.site.register(Other, OtherAdmin)

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

I can see in eg https://github.com/django/django/blob/master/django/contrib/gis/templates/gis/admin/openlayers.html that there are some references around to example {{ id }}_map.. Will id be correct when this is using an inline?

comment:4 by Tim Graham, 6 years ago

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 by Tim Graham, 6 years ago

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.

by Tim Graham, 6 years ago

Attachment: t30049.zip added

sample app to reproduce

comment:6 by Pavel Tvrdík, 6 years ago

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

comment:7 by Mariusz Felisiak, 5 years ago

Has patch: set
Version: 2.1master

comment:8 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:9 by Mariusz Felisiak, 3 years ago

GISModelAdmin is also affected.

comment:10 by Claude Paroz, 4 months ago

Needs tests: set
Owner: changed from Pavel Tvrdík to Claude Paroz

comment:11 by Claude Paroz, 4 months ago

Needs tests: unset
Patch needs improvement: unset

New PR

comment:12 by Claude Paroz, 4 months ago

Patch needs improvement: set

comment:13 by Claude Paroz, 4 months ago

I think it would be better to wait for #25706 before finishing this one.

Note: See TracTickets for help on using tickets.
Back to Top