[Answer]-How to use Dynamic fields in models?

1👍

I recommend you to use django-json-field for that purpose ( https://github.com/derek-schaefer/django-json-field ). It uses only one Text field in your db, but you can access objects in your json like a python objects:

json = JSONField() #in your model
json = "{'attributes':{'colour':'blue','size':'2'}, 'image-height':'200px'}" # in your db
{{product.json.attributes}} #in your template
product.json.attributes #in your views

It’s a great way to make many ‘django’ fields, while storing it only in one Text field in db.

The disadvantages of this approach is that you can’t effectively search for the fields using your database, but if you have a few dozens of products, you can do the filter by python right in view.

Leave a comment