Welcome | Get started | Dive into Lino | Contribute | Topics | Reference | More

Customizing your querysets

This page is a tested document and the following instructions are used for initialization:

>>> 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 view, 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 view 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 view.

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 view 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.