"add" link in TreeNodeChoiceField

43 views
Skip to first unread message

Peter Rudenko

unread,
Feb 19, 2012, 2:49:07 PM2/19/12
to django-mptt-dev
Hi, I'm using TreeNodeChoiceField in my admin model. Here is the code:

class PageAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'category':
field =
TreeNodeChoiceField(queryset=Category.objects.all())
else:
field =
super(PageAdmin,self).formfield_for_dbfield(db_field,**kwargs)
return field

But "add" link near select box disapears. How can i fix this. Thank
you for help.

Craig de Stigter

unread,
Feb 26, 2012, 9:12:24 AM2/26/12
to django-...@googlegroups.com
Sorry it's taken me a week. (I've been travelling.)

Looks like the default implementation of formfield_for_dbfield() does a few extras things after getting the formfield, including the hook for the Add button.

Instead, you should use formfield_for_foreignkey(). That way formfield_for_dbfield() will call your function and then add the extra hooks too.

If you use mptt.fields.TreeForeignKey instead of ForeignKey, it takes care of using the correct admin field for you.

HTH
Craig
Reply all
Reply to author
Forward
0 new messages