specialunderwear / django-easymode

Quickly build backends for flash/flex websites with Django.
GNU General Public License v3.0
33 stars 20 forks source link

Useful class for easymode to hide parent_link field #4

Open nnseva opened 12 years ago

nnseva commented 12 years ago

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.

specialunderwear commented 12 years ago

Cool, thanks I'll look inti it