简体   繁体   English

如何在管理更新方法中从 Django 查询集中提取 id?

[英]How do I extract id from Django queryset in admin update method?

I need to get the id of an object as I update it in the Django Admin.当我在 Django Admin 中更新对象时,我需要获取它的id

I've tried get() and have printed out the queryset using vars(queryset), but I can't seem to isolate the value I need.我试过get()并使用 vars(queryset) 打印出查询集,但我似乎无法隔离我需要的值。

I've tried to iterate over it, but to no avail.我试图迭代它,但无济于事。 I should mention I'm using MPTT, so it's a TreeQuerySet .我应该提到我使用的是 MPTT,所以它是一个TreeQuerySet But I don't think that matters.但我认为这不重要。

In admin.py:在 admin.py 中:

class JokeAdmin(MPTTModelAdmin, ImportExportModelAdmin):

    ...   ...


    def reviewed_approved(self, request, queryset):
        queryset.update(reviewed=True, approved=True)
        print(vars(queryset))

this is the output:这是输出:

{'model': class 'blog.models.Joke','_db': None,'_hints': {},'query': <django.db.models.sql.query.Query object at 0x033404D0>, '_result_cache': None, '_sticky_filter': False, '_for_write': True, '_prefetch_related_lookups': (), '_prefetch_done': False, '_known_rel
ated_objects': {}, '_iterable_class': class 'django.db.models.query.ModelIterable', '_fields': None}

我意识到我可以从请求中获取对象 ID:

        id = request.POST.get('_selected_action')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM