Re: Need help with Query: Q Objects and .extra()
Sorry for bumping this old this, but I found solution for this problem
qs = models.MyModel.objects.filter(a=1) qs |= models.MyModel.objects.filte(b=2).extra(where=[""])
I checke the generated SQL and it's working exactly as expected.
Hope this helps anyone with the same problem.
On Saturday, 14 November 2009 03:58:34 UTC+8, Info Cascade wrote:
Hi --
I need help with a query!
Currently using .extra to do full text query on the article.title field.
art_list = Article.objects.filter(status__status='PUBLISHED')
art_list = art_list.extra(
where=['title_tsv @@ plainto_tsquery(%s)'],
params=[term])
That works great.
I want to add the ability to search in a table of tags, so that I return
articles with matching titles plus articles with tags that match the
search term. That involves a OR statement, for which I believe I need
to use a Q object.
What I can't figure out is how to use the Q object with the .extra.
Additionally, I need to select distinct() because there can be more than
one matching tag.
The end SQL should look something like this:
SELECT DISTINCT article.*
FROM article JOIN article_tags ON article.id = article_tags.article_id
JOIN tag ON article_tags.tag_id = tag.id
JOIN article_status ON article_status.id = article.status_id
WHERE article_status.status = 'PUBLISHED' AND (tag.name LIKE '%volcano%'
OR title_tsv @@ plainto_tsquery('Aarhus')) ORDER BY article.title;
That gives the correct results, although it's a bit slow.
What I want might look something like this:
art_list = Article.objects.filter(status__status='PUBLISHED',
QExtra(where=['title_tsv @@ plainto_tsquery(%s)'],
params=[term])) |
Q(tags__name__icontains=term)).distinct()
I may end up doing the tag query using full text search, in which case
it would have to go in the extra(), but I'm not sure about the syntax
for doing the joins "manually" in extra().
Anyone have any suggestions on how to do this?
Liam
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/UaHhen__sBIJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home