Part 2 of this question asked and answered separately.
I have a Report
and a ReportTemplate
.
+----+----------+---------------+-------------+
| id | title | data | template_id |
+----+----------+---------------+-------------+
| 1 | report 1 | {data: [...]} | 1 |
+----+----------+---------------+-------------+reports table+----+-----------+---------------+------------+
| id | title | markup | css |
+----+-----------+---------------+------------+
| 1 | template1 | <doctype!>... | body {.... |
+----+-----------+---------------+------------+templates table
A Report belongs to a ReportTemplate. A ReportTemplate has many Report.
I have a custom admin action for Report in admin.py
called print_as_pdf
class ReportAdmin(admin.ModelAdmin):fields = ['commodity', 'date','trade_period','quantity_cutoff','data','template','title']actions = ['print_as_pdf']def print_as_pdf(self, request, queryset):returnprint_as_pdf.short_description = 'Generate as pdf'
These are models:
class ReportTemplate(models.Model):title = models.CharField(max_length=50)markup = models.TextField(default = 'markup here...')styles = models.TextField(default = 'styles here...')# __unicode__ on Python 2# __str__ on Python 3def __unicode__(self):return self.titleclass Report(models.Model):title = models.CharField(max_length=50)commodity = models.CharField(max_length=10)date = models.DateTimeField('date traded')trade_period = models.CharField(max_length=10, default='open')quantity_cutoff = models.IntegerField(default=0)printed = models.BooleanField(default=0)datetime_email_sent = models.DateTimeField('date email sent', blank=True, null=True)data = models.TextField(default = 'data here...')template = models.ForeignKey(ReportTemplate)
What I want to do is:
- retrieve the associated ReportTemplate and its
markup
field value - put the
data
field value of the Report through themarkup
value in 1 which is written with jinja2 markup - use weasyprint and print out the data-filled markup from 2 as pdf
I am stuck at step 1.
Given the parameters self
, request
, queryset
, how do I retrieve the associated ReportTemplate and its markup
field value?
UPDATE 1:
I tried this to test one of the answers given.
import logginglogger = logging.getLogger(__name__)# .... code here ...def print_as_pdf(self, request, queryset):for report in queryset:markup = report.template.markuplogger.debug(markup)return
UPDATE 2:
# Logging
LOGGING = {'version': 1,'disable_existing_loggers': False,'handlers': {'file': {'level': 'DEBUG','class': 'logging.FileHandler','filename': '/var/virtual/WebApps/virtualenvs/WeasyPrintProject/weasyprint_site/debug.log',},},'loggers': {'reports.admin': {'handlers': ['file'],'level': 'DEBUG','propagate': True,},},
}
Wrote this in my settings.py
Generated a debug.log
However, contents of debug.log are empty
UPDATE 3:
needed to explicitly change
logger = logging.getLogger(__name__)
to
logger = logging.getLogger('reports.admin')