Re: Querying a model with a ForeignKey

2016-05-09 Thread ofeyofey
This also grabs the frequency or number of views per post. On Tuesday, 3 May 2016 18:43:53 UTC+1, ofeyofey wrote: > > I have two tables in a sqlite3 DB, PostModel and TopicModel. The PostModel > has fields id, post, author, pub_date and topicid. This last fields topicid > has a foreignkey to the

Re: Querying a model with a ForeignKey

2016-05-09 Thread ofeyofey
Hi, Just in case it is useful to anyone, here is the solution, > > pModel = PostModel.objects.raw('SELECT *, max(pub_date), count(topicid_id) > AS freq FROM crudapp_postmodel GROUP BY topicid_id ORDER BY pub_date DESC > LIMIT 0,20') Thanks On Tuesday, 3 May 2016 18:43:53 UTC+1, ofeyofey wrote

Re: Querying a model with a ForeignKey

2016-05-03 Thread ofeyofey
No problem. I will post if i figure it out. Thanks very much for your help On Tuesday, 3 May 2016 18:43:53 UTC+1, ofeyofey wrote: > > I have two tables in a sqlite3 DB, PostModel and TopicModel. The PostModel > has fields id, post, author, pub_date and topicid. This last fields topicid > has a f

Re: Querying a model with a ForeignKey

2016-05-03 Thread Aaron Cannon
I fear doing this efficiently is beyond my limited abilities. Will look forward to other answers you might get. Good luck. Aaron On 5/3/16, ofeyofey wrote: > Aaron thanks for looking at this. > I understand what you did now, and that is great. > But I actually realised that the views I showed

Re: Querying a model with a ForeignKey

2016-05-03 Thread ofeyofey
Aaron thanks for looking at this. I understand what you did now, and that is great. But I actually realised that the views I showed are not the correct ones and the view I want to do this in called 'init' actually has no id. So How might I get one instance of each topic where if there is more than

Re: Querying a model with a ForeignKey

2016-05-03 Thread Aaron Cannon
Does pModel = PostModel.objects.filter(topicid_id=pk).order_by('-pub_date')[0] work for you? Also, I believe it would be more conventional if you named your topicid field simply topic, your post field as body, and your topic field as name. Luck. Aaron On 5/3/16, ofeyofey wrote: > I have

Querying a model with a ForeignKey

2016-05-03 Thread ofeyofey
I have two tables in a sqlite3 DB, PostModel and TopicModel. The PostModel has fields id, post, author, pub_date and topicid. This last fields topicid has a foreignkey to the other table Postmodel. PostModel has the fields id, topic. There is a one-to-many relation from the TopicModel to the Pos