In django admin, using django 1.2, i'm trying to add a InlineModelAdmin to apply a comment on save when a change is made to an entry. (An entry is expected to have a "ChangeComment" for every edit).
I don't want to show previous entries, so I am trying to force the ChangeCommentInline's formset.queryset to be empty, by creating NoCommentsInlineFormset and assigning the formset in my ChangeCommentInline, but is still returning existing entries.
https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#changing-the-queryset
Note - In the link above they use BaseModelFormset, I'm using BaseInlineFormset, which I expect may be the issue. If I swap out BaseInlineFormset with BaseModelFormset I get an error about "instance" not existing.
admin.py
class NoCommentsInlineFormset(models.BaseInlineFormset):
def __init__(self, *args, **kwargs):
super(NoCommentsInlineFormset, self).__init__(*args, **kwargs)
self.queryset = ChangeComment.objects.none()
class ChangeCommentInline(admin.StackedInline):
model = ChangeComment
extra = 1
exclude = ("user", )
formset = NoCommentsInlineFormset
def save_model(self, request, obj, form, change):
"""auto-assign logined in user to comment"""
if not change:
obj.user = request.user
obj.save()
class EntryAdmin(admin.ModelAdmin):
inlines = (ChangeCommentInline, )
Can limiting the ChangeComment entries displayed in the Inline be done, or is there a better way to handle this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…