Reference¶
WorkbookView¶
- class django_spreadsheet.WorkbookView¶
Base class for a Django view whose response is an xlsx workbook. You need to specify
filenameandworksheets.- filename¶
The filename of the workbook. This will be specified in the
Content-dispositionheader of the response, so it will be the filename that the browser will use to save the workbook as.
Worksheet¶
- class django_spreadsheet.Worksheet¶
Base class for worksheets.
Do not attempt to instantiate
Worksheetobjects directly. They are only meant to be instantiated automatically byWorkbookView, which will also setself.requestto the Django request object. This can come in handy inget_queryset().- model¶
Mandatory. The model that will be queried to populate the worksheet.
- name¶
Mandatory unless
get_name()is overridden. The name of the worksheet—it will be used in the worksheet tab.
- classmethod get_name()¶
Returns the name of the worksheet, which will be used in the worksheet tab. The default merely returns
name. Override if you need a more complicated workflow, such as a localized name.
- get_queryset()¶
Optional. The default returns
self.model.objects.all(). Override it if you need a different queryset.
- columns¶
Mandatory unless
get_columns()is overridden. It is a list of dictionaries that specify the columns of the worksheet. Each dictionary has the following items:- heading¶
The column heading, a string.
- value¶
A string or function describing the value the cell will have for each row.
If it is a string, then it is interpreted as a dotted path. For example, if
valueis “author.name”, then for each itemobjof the queryset the cell value will beobj.author.name.If it is a one-argument function, it receives the queryset item and returns the cell value. Thus,
"author.name"andlambda obj: obj.author.namewill have the same result when used as thevalue. In this case, prefer the first format. Use a function only for the cases when a string cannot do what you want.If the function is a two-argument function, it receives the
Worksheetobject as the first argument and the queryset item as the second argument, and returns the cell value. Thus you can specifyWorksheetmethods incolumns, for example:import django_spreadsheet from myapp import models class MyWorksheet(django_spreadsheet.Worksheet): model = models.Book name = "Books" def get_book_title(self, book): if self.request.user.is_authenticated: return self.book.title else: return "Redacted" columns = [ {"heading": "Title", "value": get_book_title}, ]
Do this only if you need to use
self; otherwise use one of the other forms.
- get_columns()¶
Returns a list of dictionaries with the format described in
columns, specifying the columns of the worksheet. The default merely returnscolumns. Override if you need a more complicated workflow.
- column_width_factor¶
Optional. A number specifying how large to make column widths.
django-spreadsheet attempts to “autofit” columns. However, in order to actually autofit columns, the spreadsheet would need to be rendered. django-spreadsheet (and the openpyxl library on which it is based) does not have rendering capabilities, therefore it cannot really autofit columns.
The best we can do is find the max character length for each column and multiply it with a number, namely
column_width_factor. The default is 1.23, which has been found with experimentation to provide good results.
- view¶
When a
WorkbookViewcreates aWorksheetobject, it creates this attribute that points to the view instance. One use case of this is if a workbook (i.e. adjango_spreadsheet.WorkbookView) has many worksheets that both need the same queries. In that case, in order to avoid making the same queries multiple times, they can be stored in the view and accessed from the worksheets.
- request¶
When a
WorkbookViewcreates aWorksheetobject, it creates this attribute that points to request object.