#18368 closed New feature (wontfix)
LayerMapping.save() should be able to set attributes on created model instances
Reported by: | 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 , 12 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
comment:2 by , 12 years ago
Resolution: | wontfix |
---|---|
Status: | closed → reopened |
@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:4 by , 12 years ago
Resolution: | → wontfix |
---|---|
Status: | reopened → closed |
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 , 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.
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.