限制为 django admin 内联显示的条目的查询集

Limit the queryset of entries displayed for a django admin Inline(限制为 django admin 内联显示的条目的查询集)
本文介绍了限制为 django admin 内联显示的条目的查询集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

解决方案

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

这篇关于限制为 django admin 内联显示的条目的查询集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)