Choosers that need the requesting user

Sometimes you require the current user to determine the choices for a field.

A tested document

This is a tested document. The following instructions are used for initialization:

>>> from lino import startup
>>> startup('')
>>> from lino.api.doctest import *

If your chooser method needs to know the current user to determine the choices for a field, include a "ar" parameter to your chooser method:

def food_choices(cls, ar):
    year_in_school = ar.get_user().year_in_school if ar is not None else None
    food = []
    for name, reserved_for in MENU:
        if (year_in_school is None) or (reserved_for is None) or year_in_school in reserved_for:
    return food

For example the chooser for the field wants to know who is asking before deciding which sites to display, because not everybody can see every site.

>>> url = '/choices/tickets/Ticket/site'
>>> show_choices("robin", url) 
>>> show_choices("luc", url)