Welcome | Get started | Dive | Contribute | Topics | Reference | Changes | More

Customizing your querysets

Side note: Code snippets (lines starting with >>>) in this document get tested as part of our development workflow. The following initialization snippet tells you which demo project is being used in this document.

>>> import lino
>>> lino.startup('lino_book.projects.avanti1.settings')
>>> from lino.api.doctest import *

The get_request_queryset() method

Lino adds the get_request_queryset() method for customizing the Django queryset used by an action request. The method exists both on the database model and on the data table, and you can override either or both, depending on your needs.

class lino.core.model.Model
classmethod get_request_queryset(cls, ar)

Return the Django queryset to be used by action request ar for any data table on this model.

class lino.core.actors.Actor
classmethod get_request_queryset(cls, ar)

Return the Django queryset to be used by action request ar for this data table.

As an application developer you may want to override this method

  • if you have customized actor parameters, then this is the place to apply them to the queryset

  • to add select_related()

  • to add user-level filtering

The default implementation of the data table method calls the model’s method, which takes cls.objects.all() and applies the different filtering and ordering options specified on the actor class using filter, exclude, known_values, simple_parameters, quick_search, order_by limit and offset.

How to override this method:

@classmethod
def get_request_queryset(cls, ar):
    qs = super().get_request_queryset(ar)
    ...
    return qs

Customized examples lino.modlib.comments.Comment, lino_xl.lib.tickets.Site and lino_xl.lib.tickets.Ticket.

When the model is abstract, this method simulates a UNION and accepts keyword arguments. This potentially unstable feature is used for lino_xl.lib.vat.IntracomPurchases and lino_xl.lib.vat.IntracomSales.