in one of my admin forms, I override a model form field (location) in order to use a different, map specific form field (olwidget). This MapField should include a layer (InfoLayerField) that displays all other model instances but the one that is being edited at the moment. Right now, it displays all the model instances (see MyModel.objects.all()) which means, if a model is edited the current location is displayed twice.
In order to achieve this, I’d have to exclude the current edited model instance from the QuerySet used in InfoLayerField (something like MyModel.objects.exclude(pk=self.instance.pk)). But since form fields are defined as static variables, I can’t access self.instance.
Is there any way to achieve this?
# models.py
class MyModel(models.Model):
name = models.CharField(max_length=200)
location = models.PointField(blank=True, null=True)
# admin.py
from olwidget.fields import MapField, EditableLayerField, InfoLayerField
from olwidget.utils import get_ewkt
class MyModelAdminForm(forms.ModelForm):
class Meta:
model = MyModel
location = MapField([
EditableLayerField({
'geometry': 'point',
'name': 'location',
}),
InfoLayerField(
[(get_ewkt(m.location), m.name) for m in MyModel.objects.all() if m.location ], {
'geometry': 'point',
'name': 'other locations',
'cluster': True,
'cluster_display': 'list',
}
)
])
class MyModelOlwidgetAdmin(admin.ModelAdmin, GeoModelAdmin):
form = MyModelAdminForm
...
Thanks for any hint’s.
I think you just need to override the
__init__on your form: