specialunderwear/django-easymode — Issue #4: Useful class for easymode to hide parent_link field
Author: nnseva
Created: 2011-12-16 11:45:37+00:00
State: open

It is useful to hide parent_link field totally while editing a sub-model using InvisibleModelAdmin. The following snippet does it.

```
from easymode.tree.admin.relation import InvisibleModelAdmin
from django.forms.widgets import HiddenInput

class HideParentModelAdmin(InvisibleModelAdmin):
def get_fieldsets(self, request, obj=None):
fss = super(HideParentModelAdmin,self).get_fieldsets(request,obj)
found = False
newfss = [(f[0],f[1].copy()) for f in fss]
for fs in newfss:
cols = []
for fcol in fs[1]['fields']:
if isinstance(fcol,basestring):
if fcol == self.parent_link:
found = True
continue
cols.append(fcol)
continue
rows = []
for frow in fcol:
if frow == self.parent_link:
found = True
continue
rows.append(frow)
if rows:
cols.append(tuple(rows))
fs[1]['fields'] = tuple(cols)
if found:
newfss.append( ('',{'fields':((self.parent_link,),),'classes':['collapse',]}) )
return newfss
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
if db_field.name == self.parent_link:
kwargs['widget'] = HiddenInput
return super(HideParentModelAdmin,self).formfield_for_foreignkey(db_field, request, **kwargs)
save_as = True
```

Just because the "Save and add another" button does not have sense in this case, it has been changed to "Save As" button.