Opened 3 weeks ago

Last modified 3 weeks ago

#37135 assigned Bug

LayerMapping unable to update PointFields with "unique" keyword parameter

Reported by: Agnès Haasser Owned by: Agnès Haasser
Component: GIS Version: 5.2
Severity: Normal Keywords:
Cc: Agnès Haasser Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello,

I'm trying to use LayerMapping in order to import Geographic Data in my database, in "update" mode. That is, I use "unique" parameter to check if LayerMapping should update existing entries or create new ones:

lm = LayerMapping(
            target_class,
            path_to_my_shp_file,
            my_mapping,
            unique=tuple_of_django_fields,
        )
lm.save(strict=True, verbose=True)

When target_class has a geometry of type PointField, I get an error:

'Point' object is not iterable
Traceback (most recent call last):
  File "file_on_disk/sia/gis_import/views.py", line 12, in launch_import
    databatch.import_geographic_data()
  File "file_on_disk/sia/gis_import/models.py", line 66, in import_geographic_data
    lm.save(strict=True, verbose=True)
  File "file_on_disk/venv/lib/python3.11/site-packages/django/contrib/gis/utils/layermapping.py", line 728, in save
    _save()
  File "file_on_disk/venv/lib/python3.11/site-packages/django/contrib/gis/utils/layermapping.py", line 653, in _save
    for g in new:
TypeError: 'Point' object is not iterable

I'm using Django 5.2 so I have an older version of layermapping but it seems the bug is still here in present source code (6.2 is not out yet as I write this ticket):

https://github.com/django/django/blob/ea9742c6d02edf64fc0969f21506f2048c976051/django/contrib/gis/utils/layermapping.py#L661-L663

Change History (3)

comment:1 by Jacob Walls, 3 weeks ago

Summary: LayerMapping utility seems unable to import PointFields with "unique" keyword parameterLayerMapping unable to update PointFields with "unique" keyword parameter
Triage Stage: UnreviewedAccepted

Thanks, reproduced at 170975c5bdc3fc69b15e46f50df7b48eb9e1115c with this rough test:

  • tests/gis_tests/layermap/tests.py

    diff --git a/tests/gis_tests/layermap/tests.py b/tests/gis_tests/layermap/tests.py
    index c590defbd0..ec8d5dc89b 100644
    a b class LayerMapTest(TestCase):  
    102102        lm.save()
    103103        self.assertEqual(City.objects.count(), 3)
    104104
     105    def test_layermap_unique_update(self):
     106        lm = LayerMapping(City, city_shp, city_mapping, unique="point")
     107        lm.save()
     108        # Update.
     109        lm.save()
     110
     111        # some assertion about the correct result...
     112
    105113    def test_layermap_strict(self):
    106114        "Testing the `strict` keyword, and import of a LineString shapefile."
    107115        # When the `strict` keyword is set an error encountered will force

Would you like submit a PR?

Version 0, edited 3 weeks ago by Jacob Walls (next)

comment:2 by Agnès Haasser, 3 weeks ago

Yes I can submit a PR, I have already written a fix in my project.

comment:3 by Agnès Haasser, 3 weeks ago

Owner: set to Agnès Haasser
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top