django formset foo to get relation inline

This commit is contained in:
Sanj 2011-09-01 06:43:26 +05:30
parent c82f7767a6
commit cd5272bfde
2 changed files with 18 additions and 2 deletions

View File

@ -85,7 +85,7 @@ class FeatureRelationInline(admin.TabularInline):
model = Feature.relationships.through model = Feature.relationships.through
extra = 1 extra = 1
fk_name = 'feature1' 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',) fields = ('preferred_name', 'feature_type', 'admin1', 'admin2', 'geometry', 'url', 'authority_record', 'time_frame', 'is_primary',)
search_fields = ['preferred_name'] search_fields = ['preferred_name']
# list_filter = ('feature_type',) # list_filter = ('feature_type',)
inlines = [FeatureNamesInline] inlines = [FeatureNamesInline, FeatureRelationInline]
list_display = ('__unicode__', 'feature_type_name', 'admin1', 'admin2', 'time_start', 'time_end',) list_display = ('__unicode__', 'feature_type_name', 'admin1', 'admin2', 'time_start', 'time_end',)
list_per_page = 30 list_per_page = 30
# list_filter = (FeatureTypeFilter,) # list_filter = (FeatureTypeFilter,)

16
gazetteer/places/forms.py Normal file
View File

@ -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)