आप # 1 के लिए Q वस्तुओं इस्तेमाल कर सकते हैं:
# Blogs who have either hockey or django tags.
from django.db.models import Q
Blog.objects.filter(
Q(tags__name__iexact='hockey') | Q(tags__name__iexact='django')
)
यूनियनों और चौराहों, मुझे विश्वास है, Django ORM के दायरे से बाहर एक सा है, लेकिन इन करने के लिए यह संभव है। निम्नलिखित उदाहरण Django एप्लिकेशन से हैं जिन्हें django-tagging कहा जाता है जो कार्यक्षमता प्रदान करता है। Line 346 of models.py:
दो भाग के लिए, आप दो प्रश्नों का एक संघ के लिए, # 3 भाग के लिए देख रहे हैं मूल रूप से
def get_union_by_model(self, queryset_or_model, tags):
"""
Create a ``QuerySet`` containing instances of the specified
model associated with *any* of the given list of tags.
"""
tags = get_tag_list(tags)
tag_count = len(tags)
queryset, model = get_queryset_and_model(queryset_or_model)
if not tag_count:
return model._default_manager.none()
model_table = qn(model._meta.db_table)
# This query selects the ids of all objects which have any of
# the given tags.
query = """
SELECT %(model_pk)s
FROM %(model)s, %(tagged_item)s
WHERE %(tagged_item)s.content_type_id = %(content_type_id)s
AND %(tagged_item)s.tag_id IN (%(tag_id_placeholders)s)
AND %(model_pk)s = %(tagged_item)s.object_id
GROUP BY %(model_pk)s""" % {
'model_pk': '%s.%s' % (model_table, qn(model._meta.pk.column)),
'model': model_table,
'tagged_item': qn(self.model._meta.db_table),
'content_type_id': ContentType.objects.get_for_model(model).pk,
'tag_id_placeholders': ','.join(['%s'] * tag_count),
}
cursor = connection.cursor()
cursor.execute(query, [tag.pk for tag in tags])
object_ids = [row[0] for row in cursor.fetchall()]
if len(object_ids) > 0:
return queryset.filter(pk__in=object_ids)
else:
return model._default_manager.none()
मैं तुम्हें एक चौराहे के लिए देख रहे विश्वास करते हैं। line 307 of models.py
def get_intersection_by_model(self, queryset_or_model, tags):
"""
Create a ``QuerySet`` containing instances of the specified
model associated with *all* of the given list of tags.
"""
tags = get_tag_list(tags)
tag_count = len(tags)
queryset, model = get_queryset_and_model(queryset_or_model)
if not tag_count:
return model._default_manager.none()
model_table = qn(model._meta.db_table)
# This query selects the ids of all objects which have all the
# given tags.
query = """
SELECT %(model_pk)s
FROM %(model)s, %(tagged_item)s
WHERE %(tagged_item)s.content_type_id = %(content_type_id)s
AND %(tagged_item)s.tag_id IN (%(tag_id_placeholders)s)
AND %(model_pk)s = %(tagged_item)s.object_id
GROUP BY %(model_pk)s
HAVING COUNT(%(model_pk)s) = %(tag_count)s""" % {
'model_pk': '%s.%s' % (model_table, qn(model._meta.pk.column)),
'model': model_table,
'tagged_item': qn(self.model._meta.db_table),
'content_type_id': ContentType.objects.get_for_model(model).pk,
'tag_id_placeholders': ','.join(['%s'] * tag_count),
'tag_count': tag_count,
}
cursor = connection.cursor()
cursor.execute(query, [tag.pk for tag in tags])
object_ids = [row[0] for row in cursor.fetchall()]
if len(object_ids) > 0:
return queryset.filter(pk__in=object_ids)
else:
return model._default_manager.none()
एक वास्तव में महान जवाब के साथ चेक बाहर [इस सवाल] (http://stackoverflow.com/q/12752601/1226722) चाल करेंगे। उपयोगी हो सकता है (मुझे पता है कि यह प्रश्न ~ 6 साल पुराना है, लेकिन मुझे अभी भी जवाब खोजने के दौरान यह मिला है!) – gregoltsov
यह एक वास्तविक एसक्यूएल यूनियन की बजाय या जहां खंड में एक मुद्दा है। यदि आप एक यूनियन की तलाश में हैं तो https: // stackoverflow पर देखें।कॉम/प्रश्न/4411049/कैसे-कर-मैं-मिल-द-यूनियन-ऑफ-दो-डीजेंगो-क्वेरीसेट – jocassid