In summary: I have a mysql db table that holds pdf objects. When a user prints one of them, I want the table to update with the time, date, printed boolean value, and the user that printed the pdf object. This user information should write the value for the user currently logged in. I have all of these currently updating when a pdf is printed with the exception of user. I have tried using request.user but that does not seem to work within a queryset or else I am missing something. I have also tried using methodology similar to how I have defined get_user, which allows me to manually set the user, but again, that is not within the queryset to update automatically. Any help is much appreciated! My admin.py file is as follows:
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)
Why not just pass
requestas an argument tocreate_pdf?