开发者

Limit the queryset of entries displayed for a django admin Inline

开发者 https://www.devze.com 2023-03-20 17:16 出处:网络
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)

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()    

开发者_如何学Python
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?


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()  


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号