I have following code:
hotels = models.Hotel.objects.filter(
wed=True,
county=hotel_main.county.id,
subscriptions__end_date__gte=datetime.date.today(),
subscriptions__enquiry_count__lte=F('subscriptions__tier__enquiry_limit'),
).filter(Q(x=TRUE|Q(y=True)).distinct()
And i have a list test = ['x','y','z','w']
The values of filter changes dynamically, so I use something like:
filter(Q(test[0]=TRUE|Q(test[3]=True))
which changes dynamically, but when I did that I got the following error:
Keyword can't be an expression
I know i can't use it there but let me know how to achieve this... Thank you!
Edit
I did as Chris suggested:
test[1] = {x: True}
and in my filter, I did as follows
filter(Q(test[1])).
it gave me below error:
need more than 1 value to unpack
Use a dictionary to set the keys and then expand it into kwargs with **, i.e.:
Q(**{test[0]: True})
Related
Really frustrated about not solving it myself but finally gave up.
I have an array of fields, each being a map with title and value. I want to iterate through it and create a list of text widgets along with some padding between them. I tried the following:
fields.expand((field)=>
[ Text(field['title']),
Padding(padding: const EdgeInsets.only(bottom: 4.0))
]
).toList()
But this gives the following error:
type 'List<dynamic>' is not a subtype of type 'List<Widget>'
I tried adding <Widget> after expand but then I get:
type 'MappedListIterable<Map<String, String>, dynamic>' is not a subtype of type 'List<Widget>'
I also tried adding <Widget> after expand( but then I get:
type '<Widget>(dynamic) => List<Widget>' is not a subtype of type '(Map<String, String>) => Iterable<dynamic>' of 'f'
Not sure what else to do. How the heck can I force it to realize this is a list of widgets?
I also tried using fold but got similar typing issues.
I think what you're looking for is .map(), but there's a cleaner way to do it :
items: [
for(var field in fields)
...[
Text(field['title']),
Padding(padding: const EdgeInsets.only(bottom: 4.0)),
]
]
Make sure your minimum SDK version is 2.6.0 or above in the pubspec.yaml
As #christopher-moore noted, adding <Widget> after expand does work. The reason I thought it was not working was that I kept getting the error, but for a different place where I still had the same statement but without that addition.
As I said in my comment, one of your solutions appears to should have work, but since it didn't I'm providing an alternate solution.
You can use a for-each loop to "manually" create an output List that you can pass to your Column widget. Ex.
List<Widget> columnList = List();
for(Map field in fields) {
columnList.add(Text(field['title']);
columnList.add(Padding(padding: const EdgeInsets.only(bottom: 4.0)));
}
I have the following object result from query set on a model as follow:
ddd = Post_Sub_Category.objects.filter(
category_name__category_name__iexact=dd
).values_list('sub_category_name', flat=True)
the query set I obtained:
<QuerySet ['car', 'spare parts', 'truck', 'motor cycle']>
then tried:
print(ddd.values('sub_category_name'))
I obtained the following result:
<QuerySet [
<Post_Sub_Category: car>,
<Post_Sub_Category: spare parts>,
<Post_Sub_Category: truck>,
<Post_Sub_Category: motor cycle>
]
How to access the values only and make list of them as string:
['car','spare parts','truck','motor cycle'].
the first query set seems that it gave me what I want. However, When I use following if statement. it does not executed:
if 'car' in ddd:
# do some thing
as you can see car should be in the list. so, I could not understand why the if statement has not been executed.
any help or suggestion?
I think you might need to index that. The values list actually looks like it returns a list. Can you try:
try:
ddd = ddd[0]
except IndexError:
# Catch index error
pass
if 'car' in ddd:
# do some thing
If that doesn't work, try explicitly converting your QuerySetList to a regular ole list, as in this question, like this:
ddd = list(ddd)
Also, this looks a little strange to me: category_name__category_name__iexact=dd. Posting your corresponding models would be helpful.
I have created several Textarea widgets in Jupyter/Python in order to capture some string inputs.
In the highlighted in yellow that you can see below, the idea is that the user puts a list of numbers here (copied from Excel) and later I need to convert this text into a list or an array that contains these numbers (an iterable object). I have no idea how to do this. See:
When I print the type of this object that is called "plus" I get this:
print(type(plus))
<class 'ipywidgets.widgets.widget_string.Textarea'>
But, I am expecting to have something like this:
plus = [454, 555]
Can I bounce some ideas off you to get this?
Thanks a lot!!!
If you have an ipywidget in general, you can observe its change and get its value as following.
foo = widgets.Textarea()
# to get the value
foo.value
# to do something on value change
def bar(change):
print(change.new)
foo.observe(bar, names=['value'])
You will then have to format the string you get from the products value, but that shouldn't be too difficult.
Hope this helps
I'm trying to get Taxonomy data by particular node.
How can I get Taxonomy Term Id by using Node object ?
Drupal ver. 8.3.6
You could do something like that:
$termId = $node->get('field_yourfield')->target_id;
Then you can load the term with
Term::load($termId);
Hope this helps.
If you want to get Taxonomy Term data you can use this code:
$node->get('field_yourfield')->referencedEntities();
Hope it will be useful for you.
PS: If you need just Term's id you can use this:
$node->get('field_yourfield')->getValue();
You will get something like this:
[0 => ['target_id' => 23], 1 => ['target_id'] => 25]
In example my field has 2 referenced taxonomy terms.
Thanks!
#Kevin Wenger's comment helped me. I'm totally basing this answer on his comment.
In your code, when you have access to a fully loaded \Drupal\node\Entity\Node you can access all the (deeply) nested properties.
In this example, I've got a node which has a taxonomy term field "field_site". The "field_site" term itself has a plain text field "field_site_url_base". In order to get the value of the "field_site_url_base", I can use the following:
$site_base_url = $node->get('field_site')->entity->field_site_url_base->value;
How to extract multiple term IDs easily if you know a little Laravel (specifically Collections):
Setup: composer require tightenco/collect to make Collections available in Drupal.
// see #Wau's answer for this first bit...
// remember: if you want the whole Term object, use ->referencedEntities()
$field_value = $node->get('field_yourfield')->getValue();
// then use collections to avoid loops etc.
$targets = collect($field_value)->pluck('target_id')->toArray();
// $targets = [1,2,3...]
or maybe you'd like the term IDs comma-separated? (I used this for programmatically passing contextual filter arguments to a view, which requires , (OR) or + (AND) to specify multiple values.)
$targets = collect($field_value)->implode('target_id', ',');
// $targets = "1,2,3"
I'm trying to use numbers as my dict key. Is there anyway to initiate the dictionary using dict() method?
This works
mydict = { '100':'hundred', '200':'two hundred'}
This doesn't work?
mydict = dict( 100='hundred' )
The error says 'keyword can't be an expression' and I couldn't find any solution.
Thank you.
I can't understand your question exactly, but you mentioned to use number as dict key right? you just directly initiate it using integer instead string like this..
a = {1:'one',100:'hundered'}
print a
{1: 'one', 100: 'hundrered'}
No, it mist be a valid python identifier, so it cannot start with a number.
You can read where i found it at here in the part about dict
https://docs.python.org/2/library/stdtypes.html#typesmapping
Like the comment above says you can use an int, since dictionaries just hash the string and you get an int anyways, in the case of an int it just hashes to itself. But it doesnt work with dict ()
On that page it shows you can do
mydict = dict (zip ([1], ["one"]))
Which is kinda ugly imo, but seems to get the job done
To use the dict method you need to feed it a list or tuple of lists or tuples.
>>> dict([(100, 'hundred'), (200, 'two hundred')])
{200: 'two hundred', 100: 'hundred'}