0

I want to use a dynamic field in django means I have one model whose one is what be specified in the other model.

I hope i can explain it to you through example.

I have one model name product and each time a new product is uploaded we have to set some attributes for that and that attributes are specified in the 2nd table means in this product model one field is dynamic which depends on the 2nd table. if i select 'x' in other table then that field is shown here and if i select 'y' then that will be shown here,So that based on that product id and attribute id I can specify the attribute value in 3rd table so that in future i can fetch that value for a specified product.

I have tried this a lot but i do not know how to use dynamic fields in django, So can anybody let me know how i can do this.

Thanks

1 Answer 1

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.

1
  • can i use more then one field( primary key and one more)of one model to another model in models.py
    – user786
    Oct 8, 2012 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.