I’ve been battling with this Django and DB issue for the last few days but am stuck now.
I have an SQLite DB with, among other things, a table with attribute_id and attribute_name. The attribute_id fields increase in 10 step increments as I may need to manually insert attributes without impacting the ids and order of existing attributes.
It currently has 186 attributes in it with ids from 10 to 1830. I don’t expect this table to ever grow beyond 200 attributes so it’s small and always will be. That may be important for performance purposes.
In another table in the same DB I have items, currently about 1000. This number will grow but I don’t expect it to ever grow beyond 100,000 items.
Every item in the table has a text field called attributes that holds an array with the attribute_id of one or more attributes. I don’t expect an item to ever have more than 10 attribute_ids in this field.
In a Django template I wish to show the attribute_name for each item in comma separated text while retaining the order of the attribute_ids in the array. That is where I’m stuck.
In a For Loop of potentially hundreds of records I currently pass the contents of {{ items.attributes }} to a Template Tag where I do the lookup to get the attribute_names belonging to the attribute_ids. The challenge is that these attribute_ids are passed as [“100”, “300”, “120”] from Template to the Template Tags operation but a lookup to find the corresponding values requires these numbers to be INTs. I can’t convert to INT if the string contains [ or ] or “. I’m currently trying to do some hacking by removing those chars from the string without any success.
Then I realised there must be a much smoother ways to do this. Any hints?
Or, am I making this all too complicated and should I just pass this small table once from the View and let the Template match them up in the For Loop without dipping into the Template Tags hundreds of times?