On 20.01.11 23:48, Juliusz Gonera wrote:
> Hi,
> 
> I have a model that has many relations and when creating a new
> object I also have to create its relations objects.
> 
> Right now I have a huge controller create action (100 lines) which
> does this, but I would like to decompose it into smaller parts. I
> thought about writing methods for the model class, e.g.
> 
> class SomeRelatedModel(Base):
>     # something
> 
> class MainModel(Base):
>     # something
> 
>     some_related_objects = relationship(SomeRelatedModel)
> 
>     def add_some_related_object(self, param1, param2):
>       # here I need to check sth in even other model
>       Session.query(SomeOtherModel).filter...
> 
>         # some more stuff and checks
> 
>       object = SomeRelatedModel()
>       object.param1 = param1
>       object.param2 = param2
>       self.some_related_objects.append(object)
> 
> Is it considered bad practice to make DB queries in model's methods?
> Or is this the way to go? How do you decompose such complex actions?
> 
> Regards,

Looks to me you should be building all those relations at mapper level
[1][2]. You can map with other classes and even arbitrary sql statements,
plus you can make some of them lazy loaded so that you won't have to
retrieve them until the very last minute.
Of course all of this will be defined in your model, making your
controller lighter.

Mariano

[1] http://www.sqlalchemy.org/docs/orm/relationships.html
[2] http://www.sqlalchemy.org/docs/orm/mapper_config.html

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to