<filename>django_tpl_loader_component_test/service/models.py
from django.core.validators import MinValueValidator
from django.db import models
from django.db.models import prefetch_related_objects
from django.apps import apps
from django.db.models import Manager, QuerySet, Count
from  .querysets import  BusManager, BusQueryset, BusDriverQueryset
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType


from django.contrib.contenttypes.fields import (
    GenericForeignKey, GenericRelation,
)

class Bus(models.Model):
    '''
    The Ref of bus in positive integer.
    '''
    ref_bus = models.PositiveIntegerField(validators=[MinValueValidator(1)], unique=True)

    #objects = BusManager.from_queryset(BusQueryset)()
 
    def __str__(self):
        return f'n {self.ref_bus}' 

    class Meta:
        verbose_name_plural = 'bus'
 

class ServiceAgentBus(models.Model):
    '''
    A service-agent is defined by an end / start date, by a bus, a bus driver,
     a departure stop as well as a destination stop.
    A bus or a conductor cannot be assigned to two service-agents simultaneously.
    A service is made up of several service-agents.
    The start and end date / time of a service are defined by all the agent-services that make it up
    TODO add @classmethod for comput times.  (Timestampable possible needed)
    '''
    name = models.AutoField(primary_key=True)
    limit = models.Q(app_label = 'service', model = 'bus') |models.Q(app_label = 'service', model = 'busdriver') 
    CT_bus = models.ForeignKey(
        ContentType,
        on_delete=models.CASCADE,
        limit_choices_to = models.Q(app_label = 'service', model = 'bus'),
        default='service | bus'
        )
    bus = models.OneToOneField('Bus',  on_delete=models.CASCADE, null=True)
    CT_busdriver = models.ForeignKey(
        ContentType,
        on_delete=models.CASCADE,
        limit_choices_to = models.Q(app_label = 'service', model = 'busdriver'),
        default='service | bus driver',
                related_name='full_name'
        )
    bus_driver = models.OneToOneField('BusDriver',  on_delete=models.CASCADE, null=True)
    bus_generic = GenericForeignKey('CT_bus', 'bus')
    bus_driver_generic = GenericForeignKey('CT_busdriver', 'bus_driver')
    place_of_departure = models.ForeignKey('PlaceOfDeparture', max_length=255,on_delete=models.CASCADE,null=True)
    place_of_arrival = models.ForeignKey('PlaceOfArrival', max_length=255, on_delete=models.CASCADE, null=True)
    departure_and_arrival_times =  models.ForeignKey('DepartureAndArrivalTime', max_length=255, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return f'Service Agent Bus {self.name}'

    class Meta:
        verbose_name_plural = 'ServiceAgentBus'
        
class BusDriver(models.Model):
    '''
    First_name and last_name are'nt proposal, we start to optimised the db via help_text)
    '''
    full_name =  models.CharField(max_length=64, unique=True)

    objects = BusManager.from_queryset(BusDriverQueryset)()
    
    def __str__(self):
        return self.full_name

class DepartureAndArrivalTime(models.Model):
    date_started = models.DateTimeField()
    date_arrived = models.DateTimeField()

    class Meta:
        verbose_name_plural = 'DepartureAndArrivalTime'

class PlaceOfDeparture(models.Model):
    place_name = models.CharField(max_length=64, unique=True)
    adress_of_departure = models.TextField(max_length=255, unique=True)


class PlaceOfArrival(models.Model):
    place_name = models.CharField(max_length=64, unique=True)
    adress_of_arrival = models.TextField(max_length=255, unique=True)


<filename>django_tpl_loader_component_test/service/models.py
from django.core.validators import MinValueValidator
from django.db import models
from django.db.models import prefetch_related_objects
from django.apps import apps
from django.db.models import Manager, QuerySet, Count
from  .querysets import  BusManager, BusQueryset, BusDriverQueryset
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType


from django.contrib.contenttypes.fields import (
    GenericForeignKey, GenericRelation,
)

class Bus(models.Model):
    '''
    The Ref of bus in positive integer.
    '''
    ref_bus = models.PositiveIntegerField(validators=[MinValueValidator(1)], unique=True)

    #objects = BusManager.from_queryset(BusQueryset)()
 
    def __str__(self):
        return f'n {self.ref_bus}' 

    class Meta:
        verbose_name_plural = 'bus'
 

class ServiceAgentBus(models.Model):
    '''
    A service-agent is defined by an end / start date, by a bus, a bus driver,
     a departure stop as well as a destination stop.
    A bus or a conductor cannot be assigned to two service-agents simultaneously.
    A service is made up of several service-agents.
    The start and end date / time of a service are defined by all the agent-services that make it up
    TODO add @classmethod for comput times.  (Timestampable possible needed)
    '''
    name = models.AutoField(primary_key=True)
    limit = models.Q(app_label = 'service', model = 'bus') |models.Q(app_label = 'service', model = 'busdriver') 
    CT_bus = models.ForeignKey(
        ContentType,
        on_delete=models.CASCADE,
        limit_choices_to = models.Q(app_label = 'service', model = 'bus'),
        default='service | bus'
        )
    bus = models.OneToOneField('Bus',  on_delete=models.CASCADE, null=True)
    CT_busdriver = models.ForeignKey(
        ContentType,
        on_delete=models.CASCADE,
        limit_choices_to = models.Q(app_label = 'service', model = 'busdriver'),
        default='service | bus driver',
                related_name='full_name'
        )
    bus_driver = models.OneToOneField('BusDriver',  on_delete=models.CASCADE, null=True)
    bus_generic = GenericForeignKey('CT_bus', 'bus')
    bus_driver_generic = GenericForeignKey('CT_busdriver', 'bus_driver')
    place_of_departure = models.ForeignKey('PlaceOfDeparture', max_length=255,on_delete=models.CASCADE,null=True)
    place_of_arrival = models.ForeignKey('PlaceOfArrival', max_length=255, on_delete=models.CASCADE, null=True)
    departure_and_arrival_times =  models.ForeignKey('DepartureAndArrivalTime', max_length=255, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return f'Service Agent Bus {self.name}'

    class Meta:
        verbose_name_plural = 'ServiceAgentBus'
        
class BusDriver(models.Model):
    '''
    First_name and last_name are'nt proposal, we start to optimised the db via help_text)
    '''
    full_name =  models.CharField(max_length=64, unique=True)

    objects = BusManager.from_queryset(BusDriverQueryset)()
    
    def __str__(self):
        return self.full_name

class DepartureAndArrivalTime(models.Model):
    date_started = models.DateTimeField()
    date_arrived = models.DateTimeField()

    class Meta:
        verbose_name_plural = 'DepartureAndArrivalTime'

class PlaceOfDeparture(models.Model):
    place_name = models.CharField(max_length=64, unique=True)
    adress_of_departure = models.TextField(max_length=255, unique=True)


class PlaceOfArrival(models.Model):
    place_name = models.CharField(max_length=64, unique=True)
    adress_of_arrival = models.TextField(max_length=255, unique=True)


The Ref of bus in positive integer. #objects = BusManager.from_queryset(BusQueryset)() A service-agent is defined by an end / start date, by a bus, a bus driver,      a departure stop as well as a destination stop.     A bus or a conductor cannot be assigned to two service-agents simultaneously.     A service is made up of several service-agents.     The start and end date / time of a service are defined by all the agent-services that make it up     TODO add @classmethod for comput times.  (Timestampable possible needed) First_name and last_name are'nt proposal, we start to optimised the db via help_text)