I have a list of products and for each product i make a GET request against a api to pull in product data. Such as current price and percent changed.
I then what to present the data in a table but i can’t find a way to loop through each product, make the request and store the data to be shown within a table.
I dont want to store the data in a model cause the data from the request will change daily, so it kinda needs to be realtime.
I’ve tried a few things, but can’t get it working.
results = []
for item in products:
data = pt.get_product_by_id(item.product_slug)
price = (data['product_data']['current_price']['GBP'])
day_change_percentage = (data['product_data']['change_percentage_24h'])
results.append({item:price,item:day_change_percentage})
What exactly are you trying and what is going wrong?
Note: Regarding the code posted, the problem there appears to be some confusion with the syntax in what you’re trying to append to results. What are you trying to append there? A dict? If so, it’s not going to work the way you’re trying because you can’t use an object as the key and you can’t have multiple instances of the same key in a dict.
Basically, for each of my users they have a list of products. I am showing that list in a table, which you very kindly helped with (you’ve basically helped build my entire app. )
As part of the list of items in the table, i am making a call out to an api that returns the current price and the change from yesterday.
So in my view i am getting the list of their products and then making the api call. I then need to take the response of each call for each product and show in the same table as the list of their products
Product Name | Current Price | % Changed |
product 1 API Reponse Api Response
product 2 API Reponse Api Response
product 3 API Reponse Api Response
I was just trying to see if i can append the reponse of each request to the api to a variable so i can reference that variable in my template.
Take a step back from Django for a moment and review what and how Python dictionaries work. Then think about how you want to represent your data within results. Then, and only then, think about how you want to build that structure within your view.
I’m suggesting you ignore all other considerations at the moment.
Take the snippet I clipped previously of the one row of your representation, and replace that text “API Response” with numbers that make sense to you, and then show the Python code that would created your desired dictionary with that sample.
Product Name | Current Price | % Changed |
product 1 20 1.5
results = dict()
for item in products:
data = pt.get_product_by_id(item.product_slug)
price = (data['product_data']['current_price']['GBP'])
day_change_percentage = (data['product_data']['change_percentage_24h'])
results.add({item.product_price:price,item.day_change_percentage:day_change_percentage})
{% for each ... %}
Product Name | Current Price | % Changed |
product 1 {{results.product_price}} {{results.day_changed_percentage}}
Im not really sure Ken.
But i see this wont work cause im not defining what the different keys are. So i need to understand how i create a new key/value for each item in products
Remember, the key names only need to be unique in a dictionary.
In this case, you’re creating multiple dictionaries, all with the same keys to make it easier to refer to them in your template.
That works so im hoping that is the correct way. My next hurdle is looping through whats in Holding and results to create the list of products in 1 table with the current price.
I assume i can do the same with results and append the output of products into the same list of dictionaries?
Close but not quite. append is a method on a list. It should be: results.append({'productName':product ...})
You can build this dictionary with more fields from your model than just the name. You could also build the dictionary with a reference to the object itself.
Example:
If product is an instance of Product, then {'product': product} is a valid dictionary.
I’m looking for the models for these and am not seeing them in a prior reply in this post.
Is there a relationship between products and holdings? If not, what you’re asking doesn’t seem to make sense to me. If there is, then you should be using that relationship to retrieve the holdings instead of the global query.