class CommentInline(admin.TabularInline):
model = Comment
form = CommentForm
class PostingAdmin(admin.ModelAdmin):
inlines = [CommentInline]
form = PostingForm
Is there any way to have CommentInline use MPTTModelAdmin?
(Also, 0.6 is out! Woo!)
--
You received this message because you are subscribed to the Google Groups "django-mptt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-mptt-d...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
form = MPTTAdminFormdef formfield_for_foreignkey(self, db_field, request, **kwargs):from mptt.models import MPTTModel, TreeForeignKeyif issubclass(db_field.rel.to, MPTTModel) \and not isinstance(db_field, TreeForeignKey) \and not db_field.name in self.raw_id_fields:defaults = dict(form_class=TreeNodeChoiceField, queryset=db_field.rel.to.objects.all(), required=False)defaults.update(kwargs)kwargs = defaultsreturn super(MPTTModelAdmin, self).formfield_for_foreignkey(db_field,request,**kwargs)
So there's your mixin. Now for the admin classes:
class MPTTModelAdmin(MPTTModelMixin, ModelAdmin): # I think this is the right order, but maybe you need to swap them"""A basic admin class that displays tree items according to their position in the tree.No extra editing functionality beyond what Django admin normally offers."""if IS_GRAPPELLI_INSTALLED:change_list_template = 'admin/grappelli_mptt_change_list.html'else:change_list_template = 'admin/mptt_change_list.html'def get_changelist(self, request, **kwargs):"""Returns the ChangeList class for use on the changelist page."""return MPTTChangeListclass MPTTInlineModelAdmin(MPTTModelMixin, admin.TabularInline):
pass
And that should be all there is to it. Now you can use MPTTInlineModelAdmin instead of the normal MPTTModelAdmin for your inline case. Your comment form should be based on MPTTAdminForm but it should be in any case.Hope this helps.
return super(MPTTModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
must bereturn super(MPTTModelMixin, self).formfield_for_foreignkey(db_field, request, **kwargs)