Subclassing models?

43 views
Skip to first unread message

BigBaaadBob

unread,
Sep 15, 2013, 9:25:38 PM9/15/13
to web...@googlegroups.com
Suppose I have a "Person" table with all the typical stuff:

db.define_table('Person',
               
Field('FirstName','string', length=40, notnull=True),
               
Field('MiddleName','string', length=40),
               
Field('LastName','string', length=40, notnull=True),
               
Field('Nickname','string', length=40),
               
Field('DateOfBirth','date', notnull=True),
               
Field('EmployeeId','string', length=10, unique=True, required=True, notnull=True),
                format
=lambda r: Fullname(r.FirstName, r.MiddleName, r.Nickname, r.LastName)
               
)


And suppose I have various special kinds of people, for exampe pilots, who have things that normal people don't have, like certificates or whatnot.

db.define_table('Pilot',
               
Field('PersonId', db.Person),
               
)

db
.define_table('Certificate',
               
Field('PilotId', db.Pilot),
               
Field('TypeId', db.CertificateType),
               
Field('Expires','date'),
               
Field('CertificateLimitations','string', length=100),
               
)


What's the proper way to do this kind of thing in the DAL?

For example:

  1. The Pilot format should be the same as the Person format, and I'd like to do that without duplicating lots of tricky lambda stuff. (BTW, Virtual fields seem worthless for use in "format"!)
  2. Accessing a Pilot's Person attributes should be as easy as accessing the Pilot's Attributes.
  3. Requires (not shown above) should work for Pilots the same way as they work for Persons.
  4. Etc.

Have I strolled way off the beaten path into the muddy weeds?


Massimo Di Pierro

unread,
Sep 15, 2013, 9:33:18 PM9/15/13
to web...@googlegroups.com
Do you mean?

db.define_table('Certificate',db.Pilot,...)

BigBaaadBob

unread,
Sep 15, 2013, 11:03:23 PM9/15/13
to
Well, I mean 'db.define_table('Pilot', db.Person,...)'.   I didn't read far enough into the documentation.....  Ooopsies!

Although it is important to know this is NOT true subclassing.  A Pilot can not be accessed as a Person, so if I have, say, Pilots and Mechanics, I can't have common code that deals with just the Person aspect of these.

Thinking through my model, however, I've decided that the Pilot-ness of a Person is the existence of a specific kind of Certificate for that person and I've decided to dispense with subclassing and deal with it that way.
Reply all
Reply to author
Forward
0 new messages