How to get the price and the id from request.POST.getlist()? - django

i tried to get two value from request.POST.getlist() what i am doing is :
for price in request.POST.getlist('price')
print(price)
what if I want to get two values with two keys i mean i want the price and the id ??
for price, id in request.POST.getlist('price','id') /something like that ???
i trying to submit the data to a form :
for prix in request.POST.getlist('prix'):
na = AssociationForm({'Prix_Unitaire':str(round(float(request.POST['prix']),2)),'Quantite':request.POST['quantite']},instance=Association())
na.save()

if your post data look like as below:
{"price":[2,3,4], "id":[1,2,3]}
for price, id in zip(request.POST.getlist('price'), reqest.POST.getlist('id')):
# do your business here

Related

AWS LogInsights query stats average of count

I have cloudwatch entries that may be group with respect to a certain field. To be clear assume that field is city. I would like to count the entries with respect to cities. This is the easy part.
fields city
|stats count(*) by city
However I also want to get maximum minimum and average of this count, but I can not. Is it possible to have such queries i.e:
fields city
|stats avg(count(*) by city)
The console return an error for such query : mismatched input 'by' expecting {SYM_COMMA, SYM_RParen}
Here's how you'd do it: You'd first get the count (that you already figured) and then get the metrics you want by calling the relevant functions like so:
fields city
|stats count(*) as cityCount, avg(cityCount), max(cityCount), min(cityCount)

How to combine ifelse and left_join

I have two data sets. First dataset (AH) has a columns: Account_Number, Account_Name, Market_Value and second data set (AH1) has Account_Number, Market_Value and Fund. I am trying to bring in Fund to the first data set from second dataset for only the Account_Numbers I want. Let's say I want to bring the Funds for the account number that starts with 2 if it does not starts with 2 then I want what it is in Account_Number column. Could you please advise how would I do that?
I tried this syntax:
ifelse(AH$Account_Number == starts_with("2"),left_join(AH,AH1, by = "Account_Number),AH$Account_Number

Kettle database lookup case insensitive

I've a table "City" with more than 100k records.
The field "name" contains strings like "Roma", "La Valletta".
I receive a file with the city name, all in upper case as in "ROMA".
I need to get the id of the record that contains "Roma" when I search for "ROMA".
In SQL, I must do something like:
select id from city where upper(name) = upper(%name%)
How can I do this in kettle?
Note: if the city is not found, I use an Insert/update field to create it, so I must avoid duplicates generated by case-sensitive names.
You can make use of the String Operations steps in Pentaho Kettle. Set the Lower/Upper option to Y
Pass the city (name) from the City table to the String operations steps which will do the Upper case of your data stream i.e. city name. Join/lookup with the received file and get the required id.
More on String Operations step in pentaho wiki.
You can use a 'Database join' step. Here you can write the sql:
select id from city where upper(name) = upper(?)
and specify the city field name from the text file as parameter. With 'Number of rows to return' and 'Outer join?' you can control the join behaviour.
This solution doesn't work well with a large number of rows, as it will execute one query per row. In those cases Rishu's solution is better.
This is how I did:
First "Modified JavaScript value" step for create a query:
var queryDest="select coalesce( (select id as idcity from city where upper(name) = upper('"+replace(mycity,"'","\'\'")+"') and upper(cap) = upper('"+mycap+"') ), 0) as idcitydest";
Then I use this string as a query in a Dynamic SQL row.
After that,
IF idcitydest == 0 then
insert new city;
else
use the found record
This system make a query for file's row but it use few memory cache

How to apply regular expression on product SKU field in magento

I have let says 3 products
P1 with SKU "xyz_1" and price "10"
P2 with SKU "xyz_2" and price "20"
P3 with SKU "xyz_3" and price "50"
I want a query in magento to get sku xyz with minimum price i.e xyz_1 only.
I want something like this select ,Regex('SKU','myexpression') as SKU to remove last part from i.e "_" part from sku and then apply filter on product collection to get product with minimum price.
Any Ideas how to handle that with Magento?
You could this regex: ^xyz. This will match any string which starts with xyz and ignore what follows.
That being said, you would need to modify your query to sort the results by price since regular expressions do not cater for numerical comparisons (less then, greater than, etc).
EDIT: As per your question, you could adapt some code from here into this:
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
// now $write is an instance of Zend_Db_Adapter_Abstract
$readresult=$write->query("SELECT SUBSTRING(sku, 0, INSTR(sku, '_')) AS SKU FROM <your table> WHERE sku REGEXP '^<your sku>' ORDER BY price asc LIMIT 1;");
SELECT INSTR('foobarbar', 'bar');

Top user of day, week, all time - best way to implement?

Suppose each user on my site has a score which increases as they use the site (like Stackoverflow) and I have a field score stored in the user profile table for each user.
Getting the top 10 users of all time is easy, just order by the score column.
I want to have "top 10 today", "top 10 this week", "top 10 of all time".
What's the best way to implement this? Do I need to store every single score change with a timestamp?
You would have to have a table that stored the increments and use a timestamp. I.E.
CREATE TABLE ScoreIncreases (
PrimaryKey UNIQUEIDENTIFIER,
UserId UNIQUEIDENTIFIER,
ScoreIncrease INT,
CreatedDate DATETIME)
Your query would then be something like
SELECT TOP 1 u.PrimaryKey, SUM(ScoreIncrease)
FROM Users u
INNER JOIN ScoreIncreases si ON si.Userid=u.PrimaryKey
WHERE DATEDIFF(day,si.CreatedDate,GETDATE()) = 0
GROUP BY u.PrimaryKey
ORDER BY SUM(ScoreIncrease) DESC