How to store a dictionary on a Django Model

0 votes
I need to store some data in a Django model. These data are not equal to

I thought about subclassing the model, but I’m trying to keep the application flexible. If I use subclasses, I’ll need to create a whole class each time I need a new kind of object, and that’s no good. I’ll also end up with a lot of subclasses only to store a pair of extra fields.

I really feel that a dictionary would be the best approach, but there’s nothing in the Django documentation about storing a dictionary in a Django model.

Any clues?

all instances of the model.
Jun 29, 2020 in Python by kartik
• 37,520 points
9,325 views

1 answer to this question.

0 votes

Hello @kartik,

If you don't need to query by any of this extra data, then you can store it as a serialized dictionary. Use repr to turn the dictionary into a string, and eval to turn the string back into a dictionary. Take care with eval that there's no user data in the dictionary, or use a safe_eval implementation.

For example, in the create and update methods of your views, you can add:

if isinstance(request.data, dict) == False:
    req_data = request.data.dict().copy()
else:
    req_data = request.data.copy()

dict_key = 'request_parameter_that_has_a_dict_inside'
if dict_key in req_data.keys() and isinstance(req_data[dict_key], dict):
    req_data[dict_key] = repr(req_data[dict_key])

Hope it helps!

answered Jun 29, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
0 answers
0 votes
1 answer

How to access a dictionary element in a Django template?

Hello @kartik, Use Dictionary Items: {% for key, value ...READ MORE

answered Jul 3, 2020 in Python by Niroj
• 82,840 points
17,666 views
0 votes
1 answer

How to look up a dictionary value with a variable in Django template?

Hii @kartik, Write a custom template filter: from django.template.defaulttags ...READ MORE

answered Aug 3, 2020 in Python by Niroj
• 82,840 points
10,720 views
0 votes
1 answer

How to filter my model on the basis of text length in django?

Hello @kartik, For Django >= 1.8 you can ...READ MORE

answered Aug 13, 2020 in Python by Niroj
• 82,840 points
4,227 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,449 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,564 views
0 votes
1 answer

How to Specify a mySQL ENUM in a Django model?

Hello @kartik, You can follow this snippet below: MAYBECHOICE ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,840 points
1,475 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP