My example might be slightly contrived, because I've modified it so that the project isn't recognizable by my employer. I'm a newer developer at a very small company.
We have an object -
Posts - that has a number of defined attributes (
content) and an unknown number of unknown attributes. These attributes are not required, but providing them helps later in the system.
These can be things like
user_account_age, etc. These are not known and new ones can be added by the system at any time. Basically, if we suddenly want to collect a new attribute when a post is created, we want to be able to do so. The attributes are configured by the end user, not the developer. In this example, it'd be by the person writing the post, not the person hosting the forum.
How can I model this in Django? The custom attributes need to be searchable (ie. find all posts where the user account is less than a month old; find all posts that have been cross posted).
The defined attributes are easy enough to model. It's the optional custom ones that I can't figure out how to model or how to do it efficiently.
My first thought, from a database perspective, was to just have a key/value table where it'd have three columns:
value. This seems inefficient though as everything would need to be a string because I can't declare a field as an IP address, integer, etc because the attribute could be anything. The query to join these to the posts would be pretty simple though. I'd just need to match on
Is there a better way to handle this?
The back end database can be anything, though we currently use a combination of postgres, mysql and redis. If there is another tool that would help model this more appropriately, though, we can adopt that.