Opened 12 years ago

Closed 12 years ago

Last modified 7 years ago

#18368 closed New feature (wontfix)

LayerMapping.save() should be able to set attributes on created model instances

Reported by: geoffhing@… Owned by: nobody
Component: GIS Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

As far as I can tell, LayerMapping only allows populating a spatial model's attributes from the vector file. I recently had a use case where I needed to set other attributes of the model instances based on information not in the vector file.

I propose allowing an additional argument to LayerMapping.save() that is a dictionary of initial field values that will be passed to the constructor of the spatial model.

That is, changing the method signature to be:

    def save(self, verbose=False, fid_range=False, step=False,
             progress=False, silent=False, stream=sys.stdout, strict=False,
             model_kwargs=None):

and then inside save() around line 523, updating the keyword arguments passed to the model constructor:

                    # Add additional keyword arguments
                    if model_kwargs:
                        kwargs.update(model_kwargs)

Change History (5)

comment:1 by Claude Paroz, 12 years ago

Resolution: wontfix
Status: newclosed

I'm not thrilled by this proposed addition. I don't see anything here that cannot be done either by field default values on the model or by updating the created objects in a second pass.

comment:2 by geoffhing@…, 12 years ago

Resolution: wontfix
Status: closedreopened

@claudep, I just want to make sure you're clear on the use case that made me request this. I'm bulk loading geometries using LayerMapping from a management command (code at https://github.com/PitonFoundation/atlas/blob/locations_places/apps/storybase_geo/management/commands/loadplaces.py). The reason I needed to be able to pass additional model attributes to LayerMapping.save() was to set a ForeignKey field to a model representing levels of a geographic hierarchy. In my use case, this field can be null, so that can't be used to identify the newly-created values. Also, geometries may be loaded at different times and for different levels of the geo hierarchy, so a default value for that field doesn't make sense.

Also, maybe I'm missing something, but I couldn't seem to find a way to retrieve the model instances created by LayerMapping.save(). Certainly, I could add a field to the model to indicate whether or not the model instances had been post-processed in a second pass, but preferred not to add fields to the model just to accommodate the occasional data load.

comment:3 by Claude Paroz, 12 years ago

And what about post_save signal?

in reply to:  3 comment:4 by geoffhing@…, 12 years ago

Resolution: wontfix
Status: reopenedclosed

Replying to claudep:

And what about post_save signal?

You're absolutely right that signals are the way to go in this case. However, I think it would have to be the pre_save signal in my case. Thanks for the helpful observation.

comment:5 by Austin Riba, 7 years ago

I still think LayerMapping.save() at the least should return the saved object. I've run into the issue where I need to save data into the created object not in the shapefile, like the original author. Unlike the OP however a signal won't work (unless I'm missing something obvious) because the context needs to come from the import script, it can't be done in isolation.

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