<small id='Xcf5q'></small><noframes id='Xcf5q'>

    <legend id='Xcf5q'><style id='Xcf5q'><dir id='Xcf5q'><q id='Xcf5q'></q></dir></style></legend>
      <bdo id='Xcf5q'></bdo><ul id='Xcf5q'></ul>

      <tfoot id='Xcf5q'></tfoot>
    1. <i id='Xcf5q'><tr id='Xcf5q'><dt id='Xcf5q'><q id='Xcf5q'><span id='Xcf5q'><b id='Xcf5q'><form id='Xcf5q'><ins id='Xcf5q'></ins><ul id='Xcf5q'></ul><sub id='Xcf5q'></sub></form><legend id='Xcf5q'></legend><bdo id='Xcf5q'><pre id='Xcf5q'><center id='Xcf5q'></center></pre></bdo></b><th id='Xcf5q'></th></span></q></dt></tr></i><div id='Xcf5q'><tfoot id='Xcf5q'></tfoot><dl id='Xcf5q'><fieldset id='Xcf5q'></fieldset></dl></div>

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

      时间:2023-11-08

      <small id='1OR0U'></small><noframes id='1OR0U'>

      <tfoot id='1OR0U'></tfoot>

        <legend id='1OR0U'><style id='1OR0U'><dir id='1OR0U'><q id='1OR0U'></q></dir></style></legend>
        <i id='1OR0U'><tr id='1OR0U'><dt id='1OR0U'><q id='1OR0U'><span id='1OR0U'><b id='1OR0U'><form id='1OR0U'><ins id='1OR0U'></ins><ul id='1OR0U'></ul><sub id='1OR0U'></sub></form><legend id='1OR0U'></legend><bdo id='1OR0U'><pre id='1OR0U'><center id='1OR0U'></center></pre></bdo></b><th id='1OR0U'></th></span></q></dt></tr></i><div id='1OR0U'><tfoot id='1OR0U'></tfoot><dl id='1OR0U'><fieldset id='1OR0U'></fieldset></dl></div>

              <tbody id='1OR0U'></tbody>

            • <bdo id='1OR0U'></bdo><ul id='1OR0U'></ul>
              • 本文介绍了限制为 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 内联显示的条目的查询集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:无法打开文件 'django-admin.py':[Errno 2] 没有这样的文件或目录 下一篇:如何在 Django 管理面板中列出所有与外键相关的对象?

                相关文章

                  <tfoot id='eW6ut'></tfoot>
                1. <i id='eW6ut'><tr id='eW6ut'><dt id='eW6ut'><q id='eW6ut'><span id='eW6ut'><b id='eW6ut'><form id='eW6ut'><ins id='eW6ut'></ins><ul id='eW6ut'></ul><sub id='eW6ut'></sub></form><legend id='eW6ut'></legend><bdo id='eW6ut'><pre id='eW6ut'><center id='eW6ut'></center></pre></bdo></b><th id='eW6ut'></th></span></q></dt></tr></i><div id='eW6ut'><tfoot id='eW6ut'></tfoot><dl id='eW6ut'><fieldset id='eW6ut'></fieldset></dl></div>

                  1. <small id='eW6ut'></small><noframes id='eW6ut'>

                    <legend id='eW6ut'><style id='eW6ut'><dir id='eW6ut'><q id='eW6ut'></q></dir></style></legend>

                      <bdo id='eW6ut'></bdo><ul id='eW6ut'></ul>