Django model complex query ( use table A.column1 as parameter to query result from table B)

18 views
Skip to first unread message

zhang rock

unread,
Oct 21, 2014, 2:16:14 PM10/21/14
to django...@googlegroups.com
Hi All,

I have a question: how to use table A.column1 as parameter to query result from table B?

I have two tables : 
 
1. UserStay
id    candidatePoiIds    selectedPoiId
1    101,102,103          100


2. POIs
id      name          address
100   starbuck     100 main st, 
101   mcdonalds  101 main st, 

i want to get all candiate POI's name and address when i fetch User stays, the following code does not work , i also tried Manager with raw SQL, but i dont' know how to pass the candidatePois to manager, can i get the string value of "candidatePois"  in model ?

class UserStays(models.Model): 
    startTime = models.IntegerField('startTime', max_length=255)
    candidatePois = models.CharField('CharField', max_length=255)
    poiRes = POIS.objects.filter(id in F('candidatePois') )
    // poiRes = POIS.objects.filter(id = 100 )
    //print poiRes.name + poiRes.address
    

Thanks
Rock

Vijay Khemlani

unread,
Oct 21, 2014, 2:44:56 PM10/21/14
to django...@googlegroups.com
I think you are trying to establish a many-to-many relationship between the models, that way it would be like this

poiRes = models.ManyToManyField(POIS)

and the candidatePois is unnecessary in that case.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/722f80db-4069-4e30-a94a-c7d58a9c6c3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages