Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Useful class for easymode to hide parent_link field #4

Open
nnseva opened this issue Dec 16, 2011 · 1 comment
Open

Useful class for easymode to hide parent_link field #4

nnseva opened this issue Dec 16, 2011 · 1 comment

Comments

@nnseva
Copy link
Contributor

nnseva commented Dec 16, 2011

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
Copy link
Owner

Cool, thanks I'll look inti it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants