Monday, December 1, 2014

Re: Constructing query

Thank you Collin !
It works :)

On Sunday, November 30, 2014 9:09:46 PM UTC+2, Collin Anderson wrote:
Hi,

This might work:

Tag.objects.filter(name__startswith='A').exclude(company=None).order_by('name').all()

Collin


On Friday, November 28, 2014 9:08:42 AM UTC-5, termopro wrote:
I have 2 models:
Companies and Tags.

A company may have several tags associated with it, so Tag contains many-to-many relation to Company.

If i'd like to get Tags starting with "A" i'd write:
tags = Tag.objects.filter(name__startswith='A').order_by('name').all()

But how do i get the list of tags (beginning with letter 'A') which are associated with at least 1 company ?

In sql it would be something like:

SELECT * FROM
tag
WHERE
name LIKE 'A%' AND id IN (SELECT tag_id FROM tag_organization)


?

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/c9beb30d-33f5-453a-ae79-dc5100801eea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment