[Grok-dev] Multiple models inside container
faassen at startifact.com
Mon Mar 21 11:53:18 EDT 2011
On 03/17/2011 01:32 PM, Lumir Jasiok wrote:
> But now I want to add Employee class, that will be similar to Store
> class. Inside Employee AddForm I want to save inside Awis context. After
> that I will not be able to recognize which ID belongs to Store object
> and Which to Employee object. How can I correctly save Store objects and
> Employees objects inside Awis container and be able to recognize them.
> Using dictionaries - self.context.stores and self.context.employees? -
> but how can I access those dictionaries from TAL?
> Or am I completely wrong and there is better way to do it?
There are different solutions.
You could create two containers and make them @grok.traversible and
store them in the Awis object, one for Employee and one for Store. You
might need to set up the sub-containers in an event handler for the add
event to make it set up right; not sure it works in the __init__(). This
would, I think, be the simplest approach and makes sure your objects are
in different containers altogether. Container making explicit subclasses
for the subcontainers, such as EmployeeContainer and StoreContainer, so
that you can create specific views for them and such later.
Alternatively you could indeed store everything withint Awis. One way to
distinguish the two is to simply use Python's isinstance() and loop
through the things in the container in some helper method:
employees = 
for value in self.values():
if isinstance(value, Employee):
This won't be very fast if you have *many* employees or stores, though,
as it'll have to loop through them each time. Instead, you could
consider using the catalog. You can then index these things in the
catalog, and look them up quickly in the index. You'd probably have to
set up a "model_type" (or whatever name you pick) attribute on the
Store and Employee classes that you can index first:
model_type = 'employee'
setting up the catalog is something you need to google for.
Note that you can still use the catalog even if you spread things into
different containers as sketched out above.
More information about the Grok-dev