Integration of an existing Sqlalchemy model with Kotti

24 views
Skip to first unread message

Andreas Jung

unread,
Jan 15, 2016, 6:29:27 AM1/15/16
to Kotti
Hi there,

I have the following existing model (experimental, can be rearranged as needed)


which is working fine from pure Python. 

Q1: what is the best way for refactoring the model in way to make it usable within Kotti

Using the model as mix-in does not work, something like

class Country(Content, model.Country):
 .....

In general I want to keep the defined relationships that are defined inside the mode in 
Kotti as well (need by Kotti related application logic and views).

Q2: Kotti seems to derive the table name for a content-type from the class name + 's'.
Is there a way to overwrite this (in particular because the plural of 'country' is 'countries'
and not 'countrys'. In general my model defines the table name as part of the decl. layer.

Andreas

davide moro

unread,
Jan 15, 2016, 6:45:08 AM1/15/16
to ko...@googlegroups.com
Hi Andreas

Bests,

davide
 

Andreas

--
You received this message because you are subscribed to the Google Groups "Kotti" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kotti+un...@googlegroups.com.
To post to this group, send email to ko...@googlegroups.com.
Visit this group at https://groups.google.com/group/kotti.
To view this discussion on the web visit https://groups.google.com/d/msgid/kotti/ab8365cf-8250-4766-a44c-2bc907a06322%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Andreas Kaiser

unread,
Jan 15, 2016, 7:03:00 AM1/15/16
to Kotti
Hi Andreas,


welcome back! :)

On 15 Jan 2016, at 12:29, Andreas Jung wrote:

> I have the following existing model (experimental, can be rearranged
> as needed)
>
> https://gist.github.com/zopyx/5ae4fd53804ca7dd29ed
>
> which is working fine from pure Python.
>
> Q1: what is the best way for refactoring the model in way to make it
> usable within Kotti
>
> Using the model as mix-in does not work, something like
>
> class Country(Content, model.Country):
> .....
>
> In general I want to keep the defined relationships that are defined
> inside the mode in Kotti as well (need by Kotti related application
> logic and views).

Not entirely sure what you want to achieve, so the following might not
suit your need.

If I see it right, you only have 1:n relationships (country -> region ->
parish -> school -> student -> score). This can perfectly be managed
within Kotti's node tree. To make this work, you'd *only* inherit from
kotti.resources.Content, like this:

class Country(Content):
""" Managed through Kotti """

__tablename__ = 'regions'

id = Column(Integer(), ForeignKey(Content.id), primary_key=True)

title could be omitted from your models, as it would be inherited from
Content already.

kotti.resources.Node gives you the parent / children relationships (via
the parent_id attribute) which is what your model does, except that it
doesn't enforce that a country can *only* contain regions *on a database
level*. It can easily achieved on the application level though. Unless
you need the former, that'd be easiest.

> Q2: Kotti seems to derive the table name for a content-type from the
> class name + 's'.
> Is there a way to overwrite this (in particular because the plural of
> 'country' is 'countries' and not 'countrys'.
> In general my model defines the table name as part of the decl. layer.

Sure, Kotti's Base class has a declared_attr for __tablename__ (see
https://github.com/Kotti/Kotti/blob/master/kotti/sqla.py#L220),
overwriting that attribute in your derived classes should be sufficient.


HTH,

Andreas

Andreas Jung

unread,
Jan 18, 2016, 7:15:46 AM1/18/16
to Kotti
Thanks, we could resolve problem 1) ourselves and hint for #2 helped a lot and worked as expected.

-aj
Reply all
Reply to author
Forward
0 new messages