I have three tables, 1-Users, 2-Softwares, 3-UserSoftwares.
if suppose, Users table having 6 user records(say U1,U2,…,U6) and Softwares table having 4 different softwares(say S1,S2,S3,S4) and UserSoftwares stores the references if a user requested for given software only.
For example: UserSoftwares(5 records) have only two columns(userid, softwareid) which references others. and the data is:
U1 S1
U2 S2
U2 S3
U3 S3
U4 S1
Now I m expecting following results:(if current login user is U2):
S1 Disable
S2 Enable
S3 Enable
S4 Disable
Here, 1st column is softwareid or name and 2nd column is status which having only two values(Enable/Disable) based on UserSoftwares table(model). Note status is not a field of any model(table).
“My Logic is:
1. loop through each software in softwares model
2. find softwareid with current login userid (U2) in UserSoftwares model:
if it found then set status=’Enable’
if not found then set status=’Disable’
3. add this status property to software object.
4. repeat this procedure for all softwares.
”
What should be the query in python google app engine to achieve above result?
since GAE’s datastore is not relational you have to model your many-to-many relationship without using joins. Here are two methods you can adapt easily to your needs.
Working example using link model method (UPDATE #1)
Link model method (recommended)
You can model a many-to-many relationship by representing each table in this way:
As you can see it is quite similiar to the relational’s way of thinking.
Key list method (alternative)
Relationships can also be modelled as list of keys:
This approach is more suited for a small number of keys since it uses a ListProperty but is faster than than the link model method above.