django-q

filter using Q object with dynamic from user?

ⅰ亾dé卋堺 提交于 2019-11-28 07:51:29
In my views.py I have a method: #...... def get_filter_result(self, customer_type, tag_selected): list_customer_filter=[] customers_filter = Customer.objects.filter(Q(type__name=customer_type), Q(active=True), Q(tag__id=tag_selected)) for customer_filter in customers_filter: customer_filter.list_authorize_sale_type = sale_type_selected(customer_filter.authorize_sale_type) list_customer_filter.append(customer_filter) return list_customer_filter **My case tag_selected is the checkbox values that user checked I have a problems with tag_selected(is the list=1,2,3,...) that pass from my url /

django dynamically filtering with q objects

心已入冬 提交于 2019-11-28 05:19:05
I'm trying to query a database based on user input tags. The number of tags can be from 0-5, so I need to create the query dynamically. So I have a tag list, tag_list, and I want to query the database: design_list = Design.objects.filter(Q(tags__tag__contains = "tag1") and Q(tags__tag__contains = "tag2") and etc. etc. ) How can I create this feature? You'll want to loop through the tag_list and apply a filter for each one. tag_list = ['tag1', 'tag2', 'tag3'] base_qs = Design.objects.all() for t in tag_list: base_qs = base_qs.filter(tags__tag__contains=t) This will give you results matching all

Django Tastypie Advanced Filtering: How to do complex lookups with Q objects

旧城冷巷雨未停 提交于 2019-11-27 12:04:48
I have a basic Django model like: class Business(models.Model): name = models.CharField(max_length=200, unique=True) email = models.EmailField() phone = models.CharField(max_length=40, blank=True, null=True) description = models.TextField(max_length=500) I need to execute a complex query on the above model like: qset = ( Q(name__icontains=query) | Q(description__icontains=query) | Q(email__icontains=query) ) results = Business.objects.filter(qset).distinct() I have tried the following using tastypie with no luck: def build_filters(self, filters=None): if filters is None: filters = {} orm

django dynamically filtering with q objects

人走茶凉 提交于 2019-11-27 05:28:08
问题 I'm trying to query a database based on user input tags. The number of tags can be from 0-5, so I need to create the query dynamically. So I have a tag list, tag_list, and I want to query the database: design_list = Design.objects.filter(Q(tags__tag__contains = "tag1") and Q(tags__tag__contains = "tag2") and etc. etc. ) How can I create this feature? 回答1: You'll want to loop through the tag_list and apply a filter for each one. tag_list = ['tag1', 'tag2', 'tag3'] base_qs = Design.objects.all(

filter using Q object with dynamic from user?

风流意气都作罢 提交于 2019-11-27 02:01:04
问题 In my views.py I have a method: #...... def get_filter_result(self, customer_type, tag_selected): list_customer_filter=[] customers_filter = Customer.objects.filter(Q(type__name=customer_type), Q(active=True), Q(tag__id=tag_selected)) for customer_filter in customers_filter: customer_filter.list_authorize_sale_type = sale_type_selected(customer_filter.authorize_sale_type) list_customer_filter.append(customer_filter) return list_customer_filter **My case tag_selected is the checkbox values

Django Tastypie Advanced Filtering: How to do complex lookups with Q objects

流过昼夜 提交于 2019-11-26 15:53:33
问题 I have a basic Django model like: class Business(models.Model): name = models.CharField(max_length=200, unique=True) email = models.EmailField() phone = models.CharField(max_length=40, blank=True, null=True) description = models.TextField(max_length=500) I need to execute a complex query on the above model like: qset = ( Q(name__icontains=query) | Q(description__icontains=query) | Q(email__icontains=query) ) results = Business.objects.filter(qset).distinct() I have tried the following using

How to dynamically compose an OR query filter in Django?

≡放荡痞女 提交于 2019-11-26 10:08:13
From an example you can see a multiple OR query filter: Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3)) For example, this results in: [<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>] However, I want to create this query filter from a list. How to do that? e.g. [1, 2, 3] -> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3)) You could chain your queries as follows: values = [1,2,3] # Turn list of values into list of Q objects queries = [Q(pk=value) for value in values] # Take one Q object from the list query = queries.pop() # Or the Q object with the ones remaining in

How to combine two or more querysets in a Django view?

醉酒当歌 提交于 2019-11-25 21:54:31
问题 I am trying to build the search for a Django site I am building, and in that search, I am searching in 3 different models. And to get pagination on the search result list, I would like to use a generic object_list view to display the results. But to do that, I have to merge 3 querysets into one. How can I do that? I\'ve tried this: result_list = [] page_list = Page.objects.filter( Q(title__icontains=cleaned_search_term) | Q(body__icontains=cleaned_search_term)) article_list = Article.objects