T
T
Tash1moto2015-10-08 16:08:04
Django
Tash1moto, 2015-10-08 16:08:04

How to write a django query?

Hello.
there is a request for a selection of colors, by the name of the product from another model

all_product_colors = Colors.objects.filter(s=all_products.get(product_name=name)).values()

at the output I get all the keys and values:
[{u's_id': 4, 'white': False, 'green': True, u'id': 8, 'yellow': False}]
But, you need to show only those keys , where the value is True.
I thought at first using regular expressions, but I'm sure that there is an easier way, tell me who knows

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2015-10-08
@Tash1moto

> Dictionary
> Regular Expressions
Is this a joke?

all_product_colors = Colors.objects.filter(s=all_products.get(product_name=name)).values()
result = [{k: v for k, v in color.items() if v is True} for color in all_product_colors]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question