Django: Filter, list, session - python

I have some question on Django and how to use it to solve the problem below.
Suppose you have this two table
Products table
---------------------------------------------------------------
| id | productName | description | id_country |
---------------------------------------------------------------
| 1 | x | fzefzzezfz | 1 |
---------------------------------------------------------------
| 2 | y | zoinojnfze | 1 |
---------------------------------------------------------------
| 3 | az | ononbonoj | 2 |
---------------------------------------------------------------
country table
-----------------------
| id | name |
-----------------------
| 1 | france |
-----------------------
| 2 | spain |
-----------------------
and these urls:
http://www.exemple.com/list/ (list all products)
http://www.exemple.com/add/ (add a new product)
http://www.exemple.com/detail/1 (print details about product with id=1)
What I want to do is to allow visitors of website to set a filter for the duration of their navigation, so for every display of the product list
only product from France or Spain will be displayed depending on the filter.
I could use french.exemple.com or spain.exemple.com to filter result but i dont want to ducplicate the code for every
subdomain.
How would handle this problem?

You've said it yourself in the question tags: use the session.
When the user chooses a country, set that value in the request.session dict; then, in each of your views, filter the products by that value.

Related

Python Pandas datasets - Having integers values in new column by making a dictionary

I am trying to output in a new column integers values (labels/classes) based on labels of another column in my dataset. Actually I did it by creating new columns (numerical column heading) for each class with boolean values in them, so then I can use these to create the new class column with numerical values. But I was trying to do it with a dictionary, which I think it is a good and faster way.
If I run a code like this:
x=df['Item_Type'].value_counts()
item_type_mapping={}
item_list=x.index
for i in range(0,len(item_list)):
item_type_mapping[item_list[i]]=i
It generates the dictionary, but then if I run:
df['Item_Type']=df['Item_Type'].map(lambda x:item_type_mapping[x])
or
df['New_column']=[item_type_mapping[item] for item in data.Item_Type]
It displays KeyError=None
Anybody know why this occurs? I think that's strange since the dictionary has been created and I can see it through my variables
Thanks
Edit 1
#Fourier
simply I have this column:
| Item_type|
| -------- |
| Nino |
| Nino |
| Nino |
| Pasquale |
| Franco |
| Franco |
and then I need the same column or a new one to display:
| Item_type| New_column |
| -------- | ---------- |
| Nino | 1 |
| Nino | 1 |
| Nino | 1 |
| Pasquale | 2 |
| Franco | 3 |
| Franco | 3 |
Your code works for me, but what you're trying to do is already provided by pandas as Categorical data.
df = pd.DataFrame({'Item_Type': list('abca')})
df['New_column'] = df.Item_Type.astype('category').cat.codes
Result:
Item_Type New_column
0 a 0
1 b 1
2 c 2
3 a 0

Translating conditional RANK Window-Function from SQL to Pandas

I’m trying to translate a window-function from SQL to Pandas, which is only applied under the condition, that a match is possible – otherwise a NULL (None) value is inserted.
SQL-Code (example)
SELECT
[ID_customer]
[cTimestamp]
[TMP_Latest_request].[ID_req] AS [ID of Latest request]
FROM [table].[Customer] AS [Customer]
LEFT JOIN (
SELECT * FROM(
SELECT [ID_req], [ID_customer], [rTimestamp],
RANK() OVER(PARTITION BY ID_customer ORDER BY rTimestamp DESC) as rnk
FROM [table].[Customer_request]
) AS [Q]
WHERE rnk = 1
) AS [TMP_Latest_request]
ON [Customer].[ID_customer] = [TMP_Latest_request].[ID_customer]
Example
Joining the ID of the latest customer request (if exists) to the customer.
table:Customer
+-------------+------------+
| ID_customer | cTimestamp |
+-------------+------------+
| 1 | 2014 |
| 2 | 2014 |
| 3 | 2015 |
+-------------+------------+
table: Customer_request
+--------+-------------+------------+
| ID_req | ID_customer | rTimestamp |
+--------+-------------+------------+
| 1 | 1 | 2012 |
| 2 | 1 | 2013 |
| 3 | 1 | 2014 |
| 4 | 2 | 2014 |
+--------+-------------+------------+
Result: table:merged
+-------------+------------+----------------------+
| ID_customer | cTimestamp | ID of Latest request |
+-------------+------------+----------------------+
| 1 | 2014 | 3 |
| 2 | 2014 | 4 |
| 3 | 2015 | None/NULL |
+-------------+------------+----------------------+
What is the equivalent in Python Pandas?
Instead of using RANK() function, you can simply using the below, and it is easy to convert.
SELECT A.ID_Customer,A.cTimeStamp,B.ID_req
FROM Customer A
LEFT JOIN (
SELECT ID_Customer,MAX(ID_req)ID_req
FROM Customer_request
GROUP BY ID_Customer
)B
ON A.ID_Customer = B.ID_Customer
Try the following query, if you are facing any issues, ask me in the comments.

groupby: how to show max(field1) and the value of field2 corresponding to max(field1)?

Let's say I have a table with 3 fields: client, city, sales, with sales being a float.
+--------+--------+-------+
| client | city | sales |
+--------+--------+-------+
| a | NY | 0 |
| a | LA | 1 |
| a | London | 2 |
| b | NY | 3 |
| b | LA | 4 |
| b | London | 5 |
+--------+--------+-------+
For each client, I would like to show what is the city with the greatest sales, and what those sales are, ie I want this output:
+--------+--------+-------+
| client | city | sales |
+--------+--------+-------+
| a | London | 2 |
| b | London | 5 |
+--------+--------+-------+
Any suggestions?
This table can be generated with:
df=pd.DataFrame()
df['client']= np.repeat( ['a','b'],3 )
df['city'] = np.tile( ['NY','LA','London'],2)
df['sales']= np.arange(0,6)
This is wrong because it calculates the 'maximum' of the city, and shows NY because it considers that N > L
max_by_id = df.groupby('client').max()
I can first create a dataframe with the highest sales, and then merge it with the initial dataframe to retrieve the city; it works, but I was wondering if there is a faster / more elegant way?
out = pd.merge( df, max_by_id, how='inner' ,on=['client','sales'] )
I remember doing something similar with cross apply statements in SQL but wouldn't know how to run a Pandas equivalent.
You need to sort by sales and then groupby client and pick first
df.sort_values(['sales'], ascending=False).groupby('client').first().reset_index()
OR
As #user3483203:
df.loc[df.groupby('client')['sales'].idxmax()]
Output:
client city sales
0 a London 2
1 b London 5

Writing to a table in a text file issue

I previously made a database of products.
Its headings appeared as shown below.
Product | Code Product | Product Price | Current Stock | Threshold Level |
One of the products I have is a Pepsi can, which is shown below.
Pepsi | 30994663 | 0.50 | 30 | 5 |
The entire text file looks exactly like this -
Product | Code Product | Product Price | Current Stock | Threshold Level | Pepsi | 30994663 | 0.50 | 30 | 5 |
iPhone | 12345670 | 5.00 | 34 | 8 |
I want to change the value of current stock after a user has purchased 1 Pepsi can.
My code appears as such so far:
stockID = 30994663
stockNew = 29
with open('SODatabaseProduct.txt','r+') as stockDatabase:
for line in stockDatabase:
if str(stockID)in line:
product=line.strip().split('|')
product[3] = str('stockNew')
This changes the third value 'current stock' to 29 in the dictionary.
I want to transfer the change to the textfile using file.write.
I tried this method.
stockDatabase.write("<Br>" + str(product))
This, however write the dictionary in code form to the text file.
I want the following result.
Product | Code Product | Product Price | Current Stock | Threshold Level | Pepsi | 30994663 | 0.50 | 29 | 5 |
iPhone | 12345670 | 5.00 | 34 | 8 |
Of course, the line I will need to edit will need to be the one with the correct Code Product, but I can't seem to find a method that fits what I need. Can someone help?

How can I filter exported tickets from database using Django?

I am working on a Django based web project where we handle tickets based requests. I am working on an implementation where I need to export all closed tickets everyday.
My ticket table database looks like,
-------------------------------------------------
| ID | ticket_number | ticket_data | is_closed |
-------------------------------------------------
| 1 | 123123 | data 1 | 1 |
-------------------------------------------------
| 2 | 123124 | data 2 | 1 |
-------------------------------------------------
| 3 | 123125 | data 3 | 1 |
-------------------------------------------------
| 4 | 123126 | data 4 | 1 |
-------------------------------------------------
And my ticket_exported table in database is similar to
----------------------------------
| ID | ticket_id | ticket_number |
----------------------------------
| 10 | 1 | 123123 |
----------------------------------
| 11 | 2 | 123124 |
----------------------------------
so my question is that when I process of exporting tickets, is there any way where I can make a single query to get list of all tickets which are closed but ticket_id and ticket_number is not in ticket_exported table? So when I run functions it should get tickets with ticket_id '3' and '4' because they are not exported in ticket_export database.
I don't want to go through all possible tickets and check one by one if their id exists in exported tickets table if I can just do it in one query whether it is raw SQL query or Django's queries.
Thanks everyone.
you can do without is_exported field:
exported_tickets = TicketsExported.objects.all()
unexported_tickets = Tickets.object.exclude(id__in=[et.id for et in exported_tickets])
but is_exported field can be useful somewhere else
Per my comment- you could probably save yourself a bunch of trouble and just add another BooleanField for 'is_exported' instead of having a separate model assuming there aren't fields specific to TicketExported.
#doniyor's answer gets you the queryset you're looking for though. In response to your raw SQL statement question: you want: unexported_tickets.query.

Categories

Resources