使用django queryset函数更新用户外键



总之:我有一个mysql数据库表,保存pdf对象。当用户打印其中一个时,我希望用时间、日期、打印的布尔值和打印pdf对象的用户来更新表。该用户信息应该为当前登录的用户写入值。当打印pdf时,除了用户之外,我所有这些都在更新。我试过使用请求。用户,但这似乎不工作在一个查询集,否则我错过了一些东西。我还尝试使用类似于定义get_user的方法,它允许我手动设置用户,但同样,这不在查询集中自动更新。任何帮助都非常感激!我的admin.py文件如下:

from django.contrib import admin
from django.contrib.auth.models import User
from djangostuff.pdf.models import ABC
from django.http import HttpResponse
import datetime, time
class ABCAdmin(admin.ModelAdmin):
    actions = ['print_selected_pdf']
    def get_user(self):
        return '%s'%(self.user.username)
    def create_pdf(self, queryset):
        response = HttpResponse(mimetype="application/pdf")
        response['Content-Disposition'] = 'attachment; filename=form.pdf'
        for obj in queryset:
            response.write(obj.form)
            ABC.objects.filter(pk=obj.pk).update(pdf_printed="1",request_time=time.strftime("%H:%M:%S"),request_date=datetime.datetime.today())
            ABC.objects.filter(pk=obj.pk).update(user=request.user)
        return response

    def print_selected_pdf(self, request, queryset):
        # prints the pdfs for those that are selected,
        # regardless if the pdf_printed field is true or false
        qs = queryset.filter(pdf_printed__exact=0)
        return self.create_pdf(qs)
    print_selected_pdf.short_description = "Print Selected PDF"
    get_user.short_description='Printed By'
    list_display=('form_no',get_user,'request_date','request_time','pdf_printed')

admin.site.register(ABC, ABCAdmin)

为什么不直接将request作为参数传递给create_pdf呢?

def create_pdf(self, request, queryset):
    ...
def print_selected_pdf(self, request, queryset):
    qs = queryset.filter(pdf_printed__exact=0)
    return self.create_pdf(request, qs)

相关内容

  • 没有找到相关文章

最新更新