librelist archives

« back to archive

Many-to-many relationship model

Many-to-many relationship model

From:
Shams Madhani
Date:
2013-08-23 @ 15:25
Dear all,

I am learning Flask and SQLAlchemy. I am creating a simple messaging app, 
where a user can send messages to multiple other users. SQLite is used as 
the data base.

To describe the model briefly, I have a 'User' class, a 'Messages' class 
and relationship table for linking a message to its recipients.

Here is the model.py: http://paste2.org/ftPaEnGw

Can someone please check if this definition is correct. When I test it 
from a python shell, I get many error when using the commands
>>>mess = mess = model.Messages(datetime.now(), 'title', 'body', 'admin')
>>>mess.receiver.append('user1')
{debug messages truncated}
AttributeError: 'str' object has no attribute '_sa_instance_state'

Perhaps I am incorrectly assisning recipients, but I am not sure. It would
be great if someone could review the model code, and help with populating 
the association table. Also how can I implement this in the view class.

Regards,
Shams Madhani

Re: [flask] Many-to-many relationship model

From:
Anthony Ford
Date:
2013-08-23 @ 15:35
Can you post the creation of user1?

At first glance, `mess.reciever.append()` is being given a string, 'user1'
instead of the actual object user1.

Try mess.reciever.append(user1), without the single quotes around user1.



Anthony Ford,
KF5IBN,
ford.anthonyj@gmail.com


On Fri, Aug 23, 2013 at 11:25 AM, Shams Madhani <s.madhani@gmx.com> wrote:

> Dear all,
>
> I am learning Flask and SQLAlchemy. I am creating a simple messaging app,
> where a user can send messages to multiple other users. SQLite is used as
> the data base.
>
> To describe the model briefly, I have a 'User' class, a 'Messages' class
> and relationship table for linking a message to its recipients.
>
> Here is the model.py: http://paste2.org/ftPaEnGw
>
> Can someone please check if this definition is correct. When I test it
> from a python shell, I get many error when using the commands
> >>>mess = mess = model.Messages(datetime.now(), 'title', 'body', 'admin')
> >>>mess.receiver.append('user1')
> {debug messages truncated}
> AttributeError: 'str' object has no attribute '_sa_instance_state'
>
> Perhaps I am incorrectly assisning recipients, but I am not sure. It would
> be great if someone could review the model code, and help with populating
> the association table. Also how can I implement this in the view class.
>
> Regards,
> Shams Madhani

Re: [flask] Many-to-many relationship model

From:
Clayton Cafiero
Date:
2013-08-23 @ 15:34
You'd expect errors running this outside of the appropriate context. 

---------------------------------------------------------

Clayton Cafiero
Desk: +1 (802) 735-0010
Pocket: +1 (802) 735-8744
Skype: clayton.cafiero
clayton.cafiero@mondaylambson.com

Monday Lambson
Quercer & Janath, Inc.
P.O. Box 4537
Burlington, VT 05406 U.S.A.
Tel.: +1 (802) 735-0010
Skype: monday.lambson
http://mondaylambson.com/










On 2013-08-23, at 11:25 AM, Shams Madhani <s.madhani@gmx.com> wrote:

> Dear all,
> 
> I am learning Flask and SQLAlchemy. I am creating a simple messaging 
app, where a user can send messages to multiple other users. SQLite is 
used as the data base.
> 
> To describe the model briefly, I have a 'User' class, a 'Messages' class
and relationship table for linking a message to its recipients.
> 
> Here is the model.py: http://paste2.org/ftPaEnGw
> 
> Can someone please check if this definition is correct. When I test it 
from a python shell, I get many error when using the commands
> >>>mess = mess = model.Messages(datetime.now(), 'title', 'body', 'admin')
> >>>mess.receiver.append('user1')
> {debug messages truncated}
> AttributeError: 'str' object has no attribute '_sa_instance_state'
> 
> Perhaps I am incorrectly assisning recipients, but I am not sure. It 
would be great if someone could review the model code, and help with pop 
ulating the association table. Also how can I implement this in the view 
class.
> 
> Regards,
> Shams Madhani

Re: [flask] Many-to-many relationship model

From:
Shams Madhani
Date:
2013-08-23 @ 20:07
Thanks everyone for the replies. This did it. Here is what I was doing 
(when testing in the python shell):

>>> u=model.User.query.filter_by(username='user1') # recipient User object
-- returned as a list
>>> mess = model.Messages(datetime.now(), 'title', 'body', 'admin') # 
instantiation of the message object
>>> db.session.add(mess)
>>> db.session.commit
>>> mess.receiver.append('user') # {Stack trace: Attribute error -- a 
string object}
>>> mess.receiver.append(u) # {Stack trace: Object error -- a list object}

Here is what SQLAlchemy was expecting, in the correct order for use in Flask view:

>>> mess = model.Messages(datetime.now(), 'title', 'body', 'admin')
>>> mess.receiver.append(model.User.query.filter_by(username='user1').first())
>>> db.session.add(mess)
>>> db.session.commit
Solved! :)
----- Original Message -----
From: Anthony Ford
Sent: 08/23/13 09:05 PM
To: flask@librelist.com
Subject: Re: [flask] Many-to-many relationship model

Can you post the creation of user1?

At first glance, `mess.reciever.append()` is being given a string, 'user1'
instead of the actual object user1.

Try mess.reciever.append(user1), without the single quotes around user1.

Anthony Ford,
KF5IBN,
 ford.anthonyj@gmail.com 
On Fri, Aug 23, 2013 at 11:25 AM, Shams Madhani < s.madhani@gmx.com > 
wrote:Dear all,

I am learning Flask and SQLAlchemy. I am creating a simple messaging app, 
where a user can send messages to multiple other users. SQLite is used as 
the data base.

To describe the model briefly, I have a 'User' class, a 'Messages' class 
and relationship table for linking a message to its recipients.

Here is the model.py: http://paste2.org/ftPaEnGw 

Can someone please check if this definition is correct. When I test it 
from a python shell, I get many error when using the commands
>>>mess = mess = model.Messages(datetime.now(), 'title', 'body', 'admin')
>>>mess.receiver.append('user1')
{debug messages truncated}
AttributeError: 'str' object has no attribute '_sa_instance_state'

Perhaps I am incorrectly assisning recipients, but I am not sure. It would
be great if someone could review the model code, and help with populating 
the association table. Also how can I implement this in the view class.

Regards,
Shams Madhani



Regards,
Sam Madhani

Re: [flask] Many-to-many relationship model

From:
Cliff Kachinske
Date:
2013-08-23 @ 16:10
I'm not a SQLAlchemy guy.

That said, it appears you are sending a string where an integer is
expected, that being the id of the user object, not the name.




On Fri, Aug 23, 2013 at 11:25 AM, Shams Madhani <s.madhani@gmx.com> wrote:

> Dear all,
>
> I am learning Flask and SQLAlchemy. I am creating a simple messaging app,
> where a user can send messages to multiple other users. SQLite is used as
> the data base.
>
> To describe the model briefly, I have a 'User' class, a 'Messages' class
> and relationship table for linking a message to its recipients.
>
> Here is the model.py: http://paste2.org/ftPaEnGw
>
> Can someone please check if this definition is correct. When I test it
> from a python shell, I get many error when using the commands
> >>>mess = mess = model.Messages(datetime.now(), 'title', 'body', 'admin')
> >>>mess.receiver.append('user1')
> {debug messages truncated}
> AttributeError: 'str' object has no attribute '_sa_instance_state'
>
> Perhaps I am incorrectly assisning recipients, but I am not sure. It would
> be great if someone could review the model code, and help with populating
> the association table. Also how can I implement this in the view class.
>
> Regards,
> Shams Madhani