I regularly get a json file filled with data. I want to put that data into models in Django. Each dictionary in the file has data matching a model that I have. What I want is to automatically read the json file and for each dictionary, take the values and put them into a model instance in Django admin.
If a particular key value pair, like a title, does not match an existing model instance, I want Django to create a model instance in the admin and fill it with that dictionary’s data.
If it does match an existing model instance, I want Django to append the data from the dictionary to the respective fields in the model instance.
I do not want to turn this json into fixtures since it does not contain all of the information that the model needs, just some of it. Plus, the model instances may already exist. I do not want to add new database entries. Therefore I do not think loaddata can do what I am looking for. I am just looking for a way to load this data into Django Admin as new models or updates to existing models.
I can write a function that loads the file easily enough, but is there a way to set this as a custom button in the Admin?
import json
def load_model_data_from_local():
with open("latest_datafile.json", mode="r", encoding="utf-8") as ldf:
latest_data = ldf.read()
ready_data = json.loads(latest_data)
# loop through the dictionaries and take the values to set to particular model fields
...
Or is there a better way to do this? Such as making an admin-only spot where I can upload the json file, and then it will read its contents into the model from there?
The file is on a local workstation. It should never be server-side. It is purely for the admin to update the models or add more if needed, and then it should be removed.
I see, so the easier way is to create a dedicated view (probably with admin-only page permissions) that has a form to upload the json file.
Are there any examples in the wild on adding data to existing models programmatically? Would I need to go through the ORM to do that?
Pretty much everywhere, starting as early as part 4 in the Django Tutorial where it shows how selecting a poll answer adds to the total for a selection.
Effectively, there are two separate steps involved in every case. Get the data, save the data.
Where data comes from - an HTML form, a JSON submission, a file, etc - has no effect on how the models are used.
Can you point me specifically to where it shows that? There are a ton of Django Tutorials out there and the ones pretty much everywhere did not help me.
I recently picked up the book Django for APIs by William S. Vincent since it had a tutorial for a Todo app with json data. However, it did not actually show how to upload a json file and have it read. Instead, that tutorial was like nearly every tutorial I have found so far and was focused on serializing an existing model into json so that it can be sent to a frontend or anywhere else as an api.
I am specifically looking for where the data comes from, since that is where my bottleneck is. There are tons of tutorials on saving data. There are not many tutorials on getting that data from a json file. I am looking for help on reading data from a json file. How the models are used is not a primary concern for me.
That’s covered in the file upload docs. That whole first example shows a form and a view that processes an uploaded file.
The view is passing the handle for the uploaded file to the handle_uploaded_file function. In that particular example, the function is writing out the uploaded data to another file, but you don’t need to do that. You can use that data being read for whatever purposes you need.
At that point in the code, it’s no longer a Django issue, it’s a Python issue for reading a file and converting it to whatever data structure needed. It would be done in the same way in any Python program.
If you know that the file is valid JSON, then json.load will create the data structure from the file. (You’ll probably want to catch / handle any exceptions if the file being uploaded is not valid JSON.)