We are doing search in django and we have two query sets: one AND queryset and one OR queryset.
q_list = [Q(x) for x in predicates if x[1]]
_filter_AND = reduce(operator.and_, q_list)
_filter_OR = reduce(operator.or_, q_list)
_queryset_AND = queryset.filter(_filter_AND)
_queryset_OR = queryset.filter(_filter_OR).exclude(id__in=[c.id for c in _queryset_AND])
queryset = list(chain(_queryset_AND, _queryset_OR))
Now, when returning, data all the data is mixed. Also, I want to have the results from AND queryset to appear first.
Would anyone know how to do that in django?
Thanks in advance!
Related
I'm stuck on a seemingly simple issue. I want to do a different queryset if sold_data is empty. Is there an effective way to do this? Or do I need to use a for loop and loop over all the listing objects and check each one?
class Listing(models.Model):
list_price = models.IntegerField()
sold_price = models.IntegerField(null=True, blank=True)
... other fields
data = Listing.objects.filter(...) # Note: I had already made other queries
if sold_price == None:
data = data.filter(list_price__gte=1)
else:
data = data.filter(sold_price__gte=1)
You can do it using Q object.
from django.db.models import Q
# your filtered queryset is in 'data' varibale
data.filter(Q(sold_price__isnull=False, sold_price__gte=1) | Q(sold_price__isnull=True, list_price__gte=1))
if you wanna check if an object is None use the is operator
I'm not sure if I did understand your question here is what I get : you wanna filter list_price if the data contains an object with empty value else filter sold_price
You can try this
if data.filter(sold_price__isnull=True).exists():
data = data.filter(list_price__gte=1)
else:
data = data.filter(sold_price__gte=1)
I'm trying to build a page that renders searched Oscar products and filters them by their Category using GET attributes. I'm overriding get_queryset and building my object list from there
class ProductSearchView(ListView):
model = Product
template_name = "productsearch/product_list.html"
queryset = Product.objects.none()
def get_queryset(self):
word_query_attr = self.request.GET.get('q', None) # word query
sqs = SearchQuerySet().models(Product).filter(Q(title__icontains=word_query_attr) |
Q(category__name__icontains=word_query_attr) |
Q(upc__icontains=word_query_attr) |
Q(description__icontains=word_query_attr))
qs = Product.objects.all()
if self.request.GET.get('cat', None):
cat_attr = self.request.GET.get('cat', None)
category = Category.objects.get(name=cat_attr)
qs = qs.filter(categories__in=category.get_children())
My question is, can I use SearchQuerySet() to filter through fields from result objects? (in this case, categories from Product objects)
If not, is there an effective way I can create a Product queryset using SearchQuerySet() results?
I've tried filtering through IDs
object_ids = [result.object.id for result in sqs]
qs = qs.filter(id__in=object_ids).distinct()
But there are two problems: it isn't scalable (as noted here) and some queries are very slow when I'm dealing with ~900 results.
I might be missing something, but the SearchQuerySet filter in your example is already restricting by category name? Could you just change that filter to exact instead of icontains and pass your cat GET param?
sqs = SearchQuerySet().models(Product).filter(
Q(title__icontains=word_query_attr) |
Q(upc__icontains=word_query_attr) |
Q(description__icontains=word_query_attr)
)
cat_attr = self.request.GET.get('cat')
if cat_attr:
sqs.filter(category__name__exact=cat_attr)
I think Haystack should let you chain filters like that.
Suppose I have a Person model that has a first name field and a last name field. There will be many people who have the same first name. I want to write a TastyPie resource that allows me to get a list of the unique first names (without duplicates).
Using the Django model directly, you can do this easily by saying something like Person.objects.values("first_name").distinct(). How do I achieve the same thing with TastyPie?
Update
I've adapted the apply_filters method linked below to use the values before making the distinct call.
def apply_filters(self, request, applicable_filters):
qs = self.get_object_list(request).filter(**applicable_filters)
values = request.GET.get('values', '').split(',')
if values:
qs = qs.values(*values)
distinct = request.GET.get('distinct', False) == 'True'
if distinct:
qs = qs.distinct()
return qs
values returns dictionaries instead of model objects, so I don't think you need to override alter_list_data_to_serialize.
Original response
There is a nice solution to the distinct part of the problem here involving a light override of apply_filters.
I'm surprised I'm not seeing a slick way to filter which fields are returned, but you could implement that by overriding alter_list_data_to_serialize and deleting unwanted fields off the objects just before serialization.
def alter_list_data_to_serialize(self, request, data):
data = super(PersonResource, self).alter_list_data_to_serialize(request, data)
fields = request.GET.get('fields', None)
if fields is not None:
fields = fields.split(',')
# Data might be a bundle here. If so, operate on data.objects instead.
data = [
dict((k,v) for k,v in d.items() if k in fields)
for d in data
]
return data
Combine those two to use something like /api/v1/person/?distinct=True&values=first_name to get what you're after. That would work generally and would still work with additional filtering (&last_name=Jones).
I want to filter my model object using two filters.
So, it can be only one filter or both or none.
My solution is to use a lot of 'if':
if _topic or _curator:
if _topic and _curator:
queryset = Article.objects.filter(topic=_topic,curator=_curator)
elif _curator:
queryset = Article.objects.filter(curator=_curator)
else # so topic is the last choice
queryset = Article.objects.filter(topic=_topic)
else
queryset = Article.objects.all()
Can someone suggest an easier way to filter?
Queryset filters are cumulative.
queryset = Article.objects.all()
if _topic:
queryset = queryset.filter(topic=_topic)
if _curator:
queryset = queryset.filter(curator=_curator)
kwargs = {}
if _topic:
kwargs[topic] = _topic
if _curator:
kwargs[curator] = _curator
queryset = Article.objects.filter(**kwargs)
I have a third-party function which gives me a filtered queryset (e.g. records with 'valid'=True) but I want to remove a particular condition (e.g. to have all records, both valid and invalid).
Is there a way to remove a filter condition to an already-filtered queryset?
E.g.
only_valid = MyModel.objects.filter(valid=True)
all_records = only_valid.**remove_filter**('valid')
(I know that it would be better to define 'all_records' before 'only_valid', but this is just an example...)
Although there is no official way to do this using filter notation, you may easily do it with Q-notation.
For example, if you ensure that third-part function returns a Q object, not a filtered QuerySet, you may do the following:
q = ThirdParty()
q = q | Q(valid=False)
And the resulting SQL conditions will be joined using OR operator.
From the docs:
Each time you refine a QuerySet, you get a brand-new QuerySet that is in no way bound to the previous QuerySet. Each refinement creates a separate and distinct QuerySet that can be stored, used and reused.
I doubt therefore, that there is a standard way to do it. You could dig into the code, see, what filter() does and try a bit. If that doesn't help, my assumption is, you're out of luck and need to re-build the query yourself.
Use this function
from django.db.models import Q
def remove_filter(lookup, queryset):
"""
Remove filter lookup in queryset
```
>>> queryset = User.objects.filter(email='user#gmail.com')
>>> queryset.count()
1
>>> remove_filter('email', queryset)
>>> queryset.count()
1000
```
"""
query = queryset.query
q = Q(**{lookup: None})
clause, _ = query._add_q(q, self.used_aliases)
def filter_lookups(child):
return child.lhs.target != clause.children[0].lhs.target
query.where.children = list(filter(filter_lookups, query.where.children))
Here's what I did in a similar case.
all_records = MyModel.objects.all()
only_valid = MyModel.objects.filter(valid=True)
only_valid.original = all_records
...
all_records = only_valid.original
Obviously this clears any other filters too so it won't be right for every case.
original_query_set = MyModel.objects.filter(**conditions)
model_class = orginal_query_set.model
new_query_set = model_class.objects.filter(**new_conditions)
You can use the .model attribute on a QuerySet to get the model class, then use the model class to make a brand new QuerySet.
Thanks for making me check the source code Boldewyn. So, it seems there's a new method right below filter() with the same parameters... called exclude() (as of commit mini-hash ef6c680, for when it loses its line number)
Return a new QuerySet instance with NOT (args) ANDed to the existing set.
So, to answer the original question:
only_valid = MyModel.objects.filter(valid=True)
filtered_results = only_valid.exclude(the_condition_to_remove=True)