Django e-commerce website shopping cart design

Time:2021-5-31

1. Because the shopping cart is a high-frequency refresh part, and the requirements for data relational structure are not very complex, NoSQL is selected as the storage tool;
2. The front end and the back end need to dock four parameters, which are user ID and commodity code, namely SKU_ ID, how many items count and whether selected is checked;
3. Redis has five data types: string, list, hash, set and Zset. The number of the four parameters exceeds the control range of a single data structure, so two data structures are selected for common maintenance;
4. Hash structure is adopted for user ID, skuid and count
cart_user_id:{sku_id1:count,sku_id2:count,sku_id3:count….}
5. The user ID and the selected items are maintained by set, because the shopping cart does not require sequence, and the set structure can automatically remove duplicate,
select_user_id:[sku_id1,sku_id3…..]
6. When maintaining the shopping cart data in the later stage, the two tables are performed at the same time, for example, deleting the SKU in hash_ In Id1, the contents in selected should be deleted at the same time.
7. Each time you add a product in hash, you first traverse whether the product in the shopping cart already exists. If it already exists, you will accumulate count. If there is no new product added. Using the hincrby function

8. When you enter the settlement page, you need to settle the checked goods. Therefore, you need to combine the two redis tables to construct a dictionary called new_ cart_ dict = {sku_ id:count}
9. Traverse select_ user_ ID, traversed SKU_ ID is the key of the new table, and then use this key to query cart_ user_ Same SKU in ID table_ ID and then give its count to the new table

new_cart_dict = {}
for selected_sku_id in select_user_id:

new_cart_dict[int(selected_sku_id)] =  int(cart_user_id[sku_id])

10. Then enter the submit order page, that is, in the new_ cart_ Dict extracts the information and quantity of the selected product, and then responds to the front page. In addition, it finds the user’s undeleted address according to the request. User and responds to the front page together.
11. In addition, the data that needs to be responded to the front end usually includes the total number of pieces, total price, freight, total price, etc. through context = {addresses’: ‘,’skus’:’,’total ‘,’_ count’:”,’total_ Amount ‘:’,’freedom ‘:’} structure