[Grok-dev] Modeling a class that extend from list() with megrok.rdb

Jeff Peterson bgpete at gmail.com
Tue Oct 26 01:46:09 EDT 2010


How would you model it in MySQL? Because that is essentially what you are doing. Megrok.rdb is more or less sqlalchemy grokified.

Think about how it would look in MySQL then write classes subclassing rdb.Model, define columns etc.

class MyEntry(rdb.Model):

    field1 = Column('field1', String, ...)
    field2 = Column('field2', String, ...)

    def append(self, entry):
        """ code to add an entry """

Maybe I am missing something but it seems fairly straightforward.

--
Jeff Peterson

Sent from my iPod

On Oct 25, 2010, at 5:02 PM, Hector Blanco <white.lists at gmail.com> wrote:

> Hi list!
> 
> I am trying to migrate my application from ZopeDB to MySql. I am using
> megrok.rdb.
> 
> I have a class which inherits from list() and has a few extra methods.
> 
> That class acts as a container of other classes and is used (as an
> attribute) in other classes as well:
> 
> class MyEntry(object):
>    def __init__(self):
>        self.field1 = “field1”
>        self.field2 = “field2”
> 
> class MyContainer(list):
>    def __init__(self):
>        super(MyContainer,self).__init__()
> 
>    def append(myEntry):
>        if isinstance(myEntry, MyEntry):
>            super(MyContainer, self).append(myEntry)
> 
>    def getByField1(self, field1):
>        for element in self:
>            if element.field1 == field1:
>                return element
>        return None
>    
>    [ ... more useful methods ...]
> 
> class MyClass(object):
>    def __init__(self):
>        self.container1 = MyContainer()
>        self.container2 = MyContainer()
>        self.anotherField = “hello world”
>    def getContainer1():
>        return self.container1
>    def getContainer2():
>        return self.container2
> 
> I don't really know how to model this with megrok.rdb. I kind of see
> clearly (although I may be totally wrong) that MyEntry and MyClass
> should extend from rdb.Model and each of them should be stored in a
> table, but I don't really know what to do with “MyContainer”. I'd like
> to keep it inheriting from list, but I don't know how to model that.
> It really only needs to be an association between [id, id_of_my_entry]
> (relationship 1:N). In MyClass, each of the containers would get an id
> and then, if I want to get the “MyEntries” that are in container1, I
> just need to get the MyEntries who are linked to that id.
> 
> … but I have no idea about how to implement that... Any hint I could
> get would be deeply appreciated.
> 
> Thank you in advance!
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> https://mail.zope.org/mailman/listinfo/grok-dev


More information about the Grok-dev mailing list