About virtual fields¶
- database field
A regular Django model field. CharField, ForeignKey, IntegerField, TextField, BooleanField, etc.
- virtual field
A data element whose value isn't stored in the database but computed on the fly each time we want to see it. For example the
ageof a person is usually computed from the
birth_data. It wouldn't make sense to store this number in the database.
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)
Define a virtual field on a table:
@dd.virtualfield(dd.ForeignKey('polls.Question')) def question(self, obj, ar): return obj
Some entries to the API:
dd.virtualfield, dd.displayfield etc
Write a simple example project in tutorials
Some edge cases¶
A cool example is in
lino_welfare.modlib.pcsw.models where we
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
Note that actually it is abstract only in eupen, not in chatelet. But that's another cool thing.