From cd5272bfde72002cafb77b24fb8bbf043425ecf3 Mon Sep 17 00:00:00 2001 From: Sanj Date: Thu, 1 Sep 2011 06:43:26 +0530 Subject: [PATCH] django formset foo to get relation inline --- gazetteer/places/admin.py | 4 ++-- gazetteer/places/forms.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 gazetteer/places/forms.py diff --git a/gazetteer/places/admin.py b/gazetteer/places/admin.py index 04b23c1..e9723fd 100644 --- a/gazetteer/places/admin.py +++ b/gazetteer/places/admin.py @@ -85,7 +85,7 @@ class FeatureRelationInline(admin.TabularInline): model = Feature.relationships.through extra = 1 fk_name = 'feature1' - + formset = RelationFormSet @@ -93,7 +93,7 @@ class FeatureAdmin(admin.OSMGeoAdmin): fields = ('preferred_name', 'feature_type', 'admin1', 'admin2', 'geometry', 'url', 'authority_record', 'time_frame', 'is_primary',) search_fields = ['preferred_name'] # list_filter = ('feature_type',) - inlines = [FeatureNamesInline] + inlines = [FeatureNamesInline, FeatureRelationInline] list_display = ('__unicode__', 'feature_type_name', 'admin1', 'admin2', 'time_start', 'time_end',) list_per_page = 30 # list_filter = (FeatureTypeFilter,) diff --git a/gazetteer/places/forms.py b/gazetteer/places/forms.py new file mode 100644 index 0000000..5219864 --- /dev/null +++ b/gazetteer/places/forms.py @@ -0,0 +1,16 @@ +from django.forms.models import modelformset_factory +from django.forms.models import BaseModelFormSet +from ajax_select.fields import AutoCompleteSelectMultipleField, AutoCompleteSelectField + +from models import Relationship + +# create a superclass +class BaseRelationsFormSet(BaseModelFormSet): + + # that adds the field in, overwriting the previous default field + def add_fields(self, form, index): + super(BaseTaskFormSet, self).add_fields(form, index) + form.fields["feature_to"] = AutoCompleteSelectField('feature', required=False) + +# pass in the base formset class to the factory +RelationFormSet = modelformset_factory(,fields=('feature_to','relationship_type'),extra=1,formset=BaseRelationsFormSet)