Friday, November 28, 2014

Constructing query

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/971edb11-b972-4163-83bf-90f0435985fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment