I am building a chatbot using Django with a MySQL backend.
I have a ChatSession
model which represents all the useful information to be stored after the end of a chat session .
Now, it has a JSON field called cc_data
(Implemented using django-jsonfield
)
Here's an example of the content of that field:
{
"slots":{
"chal_tech":"What is the Proof",
"nats":["I will fail the course","Nobody likes me"],
"distortion":"Jumping to Conclusions",
"user_name":"parth",
}
}
I recently realized that I need to query for some fields in this slots
dictionary. An example query would be to list out all the nats
in the last 10 chat sessions.
It seems that reading all the values of the JSONs into dicts and then searching/manipulating them would be inefficient.Therefore I sought to convert this JSON representation into Django ORM models(SQL).
However, while trying to do this I ran into a problem. The "keys" of the slots
dictionary are probably finite in number, but I don't know exactly how many of them will be required at this point. In any case, it would be a large number (around 50-100).
- Manually writing the model classes for so many attributes in
models.py
seems inefficient and also a bit repetitive (Most of them would simply have a singleJSONfield
orTextField
). Even with abstract models I would still have to list out classes for all the "keys" in themodels.py
file. - Ideally I'd like to generate model classes for any new "keys" automatically and on the fly. I did look at dynamic models in Django but none of the solutions really seem efficient. The host I am using doesn't support NoSQL so I can't use that solution.
How do I convert this into valid a valid Django ORM/SQL design so that the queries are efficient and the code isn't repetitive ?