43👍
✅
Use the json module:
import json
json_data = open('/static/prices.json')
data1 = json.load(json_data) # deserialises it
data2 = json.dumps(data1) # json formatted string
json_data.close()
See here for more info.
As Joe has said, it’s a better practice to use fixtures or factories for your test data.
👤DanS
9👍
The trick here is to use python’s built-in methods to open
that file, read its contents and parse it using the json
module
i.e.
import json
data = open('/static/prices.json').read() #opens the json file and saves the raw contents
jsonData = json.loads(data) #converts to a json structure
- All the values of the many to many field : Django
- Should I use Celery or Carrot for a Django project?
- How to upgrade Django on ubuntu?
- How to give initial value in modelform
- Django-rest-swagger nested serializers with readonly fields not rendered properly
4👍
You should use Django fixtures for this.
https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs
👤Joe
- (gcloud.app.deploy) Error Response: [7] Access Not Configured. Cloud Build has not been used in project
- How do I use perform_create to set a field automatically in Django Rest Framework?
- Django aggregation: sum then average
- How can I test if my redis cache is working?
Source:stackexchange.com