About virtual fields

Recipes

Define a virtual field on a model:

@dd.virtualfield(models.IntegerField(_("Bus")))
def bus_needed(self, ar):
    return self.get_places_sum(
        state=EnrolmentStates.requested, needs_bus=True)

(Taken from lino_avanti.lib.avanti.Client)

Define a virtual field on a table:

@dd.virtualfield(dd.ForeignKey('polls.Question'))
def question(self, obj, ar):
    return obj

(Taken from lino_xl.lib.polls.PollResult)

Reference

Some entries to the API:

TODO:

  • Write a simple example project in tutorials

Some edge cases

A cool example is in lino_welfare.modlib.pcsw.models where we have:

dd.update_field(Client, 'overview', verbose_name=None)

This is special because Client is abstract at this place1. Abstract models don't have a copy of each inherited virtual field. the overview field is

1

Note that actually it is abstract only in eupen, not in chatelet. But that's another cool thing.