13

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?

monkut
  • 42,176
  • 24
  • 124
  • 155
  • Have a look at BaseModelAdmin: def queryset(self, request): """ Returns a QuerySet of all model instances that can be edited by the admin site. This is used by changelist_view. """ – benjaoming Jul 15 '11 at 16:03
  • 1
    ...what you need to do is to overwrite the queryset method on your InlineModelAdmin. – benjaoming Jul 15 '11 at 16:05
  • ok, I'll give it a try. thanks! (go ahead and add it as an answer if you want more rep) – monkut Jul 15 '11 at 16:44

2 Answers2

14

As benjaoming mentioned in the comments, it was necessary to override the get_queryset() method in the InlineModelAdmin. It was not necessary to override and attach a new formset to the InlineModelAdmin definition as I initially thought.

Here is the resulting implementation:

class ChangeCommentInline(admin.StackedInline):
    """For allowing logged in user to add change comment"""
    model = ChangeComment
    extra = 1
    exclude = ("user", ) # auto-update user field in save_formset method of parent modeladmin.


    def get_queryset(self, request):
        """Alter the queryset to return no existing entries"""
        # get the existing query set, then empty it.
        qs = super(ChangeCommentInline, self).get_queryset(request)
        return qs.none()  
piro
  • 13,378
  • 5
  • 34
  • 38
monkut
  • 42,176
  • 24
  • 124
  • 155
3

I am supposing you are using a

models.ForeignKey(EntryAdmin)

in your ChangeComment model. but if you want only one comment for each EntryAdmin, your should use instead a:

models.OneToOneField(EntryAdmin)

And you won't need your NoCommentsInlineFormset nor your inline class. That's what I would do.

EDITED

Ok then if you want to keep a history of comments, you could override the queryset in the NoCommentsInlineFormset as:

def __init__(self, *args, **kwargs):
    super(NoCommentsInlineFormset, self).__init__(*args, **kwargs)
    self.queryset = ChangeComment.objects.order_by('-created_at')[:1]

This should work.

Geoffroy CALA
  • 931
  • 7
  • 8
  • I'm trying to have a "comment" for *each* edit of the given ForeignKey Entry, and maintain that comment history. So if 5 changes were made, there would be 5 ChangeComment entries in the db, one for each edit. If I changed it to a OneToOneField wouldn't it only allow a single comment per entry? – monkut Jul 15 '11 at 14:52
  • Yes. I think then the simpler solution would be to override the queryset of your form and make something like I did in my edited answer. – Geoffroy CALA Jul 15 '11 at 15:10
  • I tried to override the queryset as you mentioned, but I got the error, "queryset" not callable. Turns out the queryset *method* in the InlineModelAdmin needed to be overridden. – monkut Jul 19 '11 at 03:20