[Schevo-devel] [TUTORIAL] - Create and Evolve a Simple OO Application

Patrick K. O'Brien pobrien at orbtech.com
Wed Feb 23 19:30:59 EST 2005


Ilias Lazaridis wrote:
> Matthew Scott wrote:
> 
>> Ilias Lazaridis wrote:
>>
>>> I'm sorry, "this" refered to "1:n relations" within schevo.
>>>
>>> how do I model them?
>>
>>
>> The "people" example in the repository is an example of modelling
>> one-to-many relationships.
>>
>> This was detailed just this morning.  In case you overlooked that
>> message, here is a link to it in the list archive:
>>
>> http://lists.orbtech.com/pipermail/schevo-devel/2005-February/000132.html
> 
> 
> I extract the answer:
> 
> add "car = f.entity(allow=Car)" to class "Driver".

I would model it thusly:

class Driver:
    """Person who currently drives a car."""

    person = f.entity(allow=Person)
    car = f.entity(allow=Car)
    licenseType = f.string()

    _key(person, car)

    _icon('.apps.personal')

    # This _plural is redundant as Schevo generates this form already.
    # Only specify a plural form if Classname + s is bad, such as:
    # Countrys (Countries), Persons (People), Companys (Companies)

    # _plural('Drivers')

    def __str__(self):
        return '%s %s' % (self.person, self.car)

The way I've modeled it is actually an example of a N:M between Cars and
People in the context of driving.  If a person can only drive one car at
a time, change the _key() to this:

    _key(person)

Now you'd have a 1:N relationship between Person and Car.  If a car can
only have one driver at a time, change the _key() to this:

    _key(car)

Now you'd have a 1:N relationship between Car and Person.  If a car can
only have one driver at a time, and at the same time a person can only
drive one car at a time, change the keys to this (full class shown for
clarity):

class Driver:
    """Person who currently drives a car."""

    person = f.entity(allow=Person)
    car = f.entity(allow=Car)
    licenseType = f.string()

    _key(person)
    _key(car)

    _icon('.apps.personal')

    def __str__(self):
        return '%s %s' % (self.person, self.car)

-- 
Patrick K. O'Brien
Orbtech    http://www.orbtech.com
Schevo     http://www.schevo.org
Pypersyst  http://www.pypersyst.org




More information about the Schevo-devel mailing list