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

The label of the actor

This document describes the lino_book.projets.actors demo project, which shows different ways of specifying the label of an actor.

>>> from lino import startup
>>> startup('lino_book.projects.actors.settings')
>>> from lino.api.doctest import *
>>> globals().update(rt.models.actors.__dict__)

It was started as a doctest for 2013-09-07.

Here is the models.py file we will use for this tutorial:

from django.db import models
from lino.api import dd


class PartnerType(dd.Model):
    name = models.CharField("Name", max_length=20)

    def __str__(self):
        return self.name


class Partner(dd.Model):

    class Meta:
        verbose_name = "Partner"
        verbose_name_plural = "Partners"

    type = dd.ForeignKey(PartnerType)
    name = models.CharField("Name", max_length=30)


class Person(Partner):

    class Meta:
        verbose_name = "Person"
        verbose_name_plural = "Persons"

    first_name = models.CharField("First name", max_length=20)
    last_name = models.CharField("Last name", max_length=20)


from .ui import *

And the ui.py file:

from .models import dd, PartnerType, Partner, Person


class Partners(dd.Table):
    model = Partner
    # no explicit `label` attribute, so take verbose_name_plural from model


class Persons(Partners):
    model = Person
    # no explicit `label` attribute, so take verbose_name_plural from model


class FunnyPersons(Persons):
    label = "Funny persons"


class MyFunnyPersons(FunnyPersons):
    # no explicit `label` attribute, so inherit from parent
    pass


PARTNER_TYPE_CUSTOMER = 1
PARTNER_TYPE_PROVIDER = 2


class TypedPartners(Partners):
    partner_type_pk = None

    @classmethod
    def make_instance(cls, **kw):
        kw.update(type_id=cls.partner_type_pk)
        obj = cls.model(**kw)
        return obj

    @classmethod
    def get_request_queryset(cls, ar):
        # override
        qs = super().get_request_queryset(ar)
        return qs.filter(type__id=cls.partner_type_pk)

    @classmethod
    def get_actor_label(cls):
        # override
        if cls.partner_type_pk is None:
            return super(TypedPartners, cls).get_actor_label()
        pt = PartnerType.objects.get(id=cls.partner_type_pk)
        return pt.name


class Customers(TypedPartners):
    partner_type_pk = PARTNER_TYPE_CUSTOMER


class Providers(TypedPartners):
    partner_type_pk = PARTNER_TYPE_PROVIDER

The label of an Actor

If a Table has no explicit label attribute, then it takes the verbose_name_plural meta option of the model:

>>> print(Partners.label)
Partners
>>> print(Persons.label)
Persons

You may specify an explicit constant label attribute:

>>> print(FunnyPersons.label)
Funny persons

In versions after 2013-09-07 this explicit label attribute is also inherited to subclasses:

>>> print(MyFunnyPersons.label)
Funny persons

Dynamic actor labels

>>> rt.show(PartnerType)
==== ===============
 ID   Name
---- ---------------
 1    Our customers
 2    Our providers
==== ===============
>>> print(Customers.label)
Our customers
>>> print(Providers.label)
Our providers
>>> rt.show(Partners)
==== =============== ==========
 ID   partner type    Name
---- --------------- ----------
 1    Our customers   Adams
 2    Our customers   Bowman
 3    Our providers   Carlsson
 4    Our customers   Dickens
==== =============== ==========
>>> rt.show(Customers)
==== =============== =========
 ID   partner type    Name
---- --------------- ---------
 1    Our customers   Adams
 2    Our customers   Bowman
 4    Our customers   Dickens
==== =============== =========
>>> rt.show(Providers)
==== =============== ==========
 ID   partner type    Name
---- --------------- ----------
 3    Our providers   Carlsson
==== =============== ==========