Reputation: 30421
Since Postgres supports JSON fields, is that more preferential than say saving each config item in a row in some table the way WP still does?
Full disclosure: I'm asking as a Django user new to postgres.
Upvotes: 0
Views: 239
Reputation: 51988
For storing configs, maybe its even better to use something like Django Constance. You can use it like this:
Install django constance using pip install django-constance[database]
.
Add constance
in INSTALLED_APPS
:
INSTALLED_APPS = (
...
'constance',
)
Add CONSTNCE_CONFIG
in settings.py
CONSTANCE_CONFIG = {
'THE_ANSWER': (42, 'Answer to the Ultimate Question of Life, '
'The Universe, and Everything'),
}
I am suggesting to use constance, because you can easily use it in your code:
from constance import config
if config.ANSWER == 44:
# do something
And its singleton, so all the constance instances will be same across the code. Which is very important to IMO because they are configurations and it should be consistent.
Also, you can access the constance variables in Adminsite and customize it there.
Upvotes: 0
Reputation: 6927
It depends. Here are some things to think about:
Does the data structure map well to JSON? Try to visualize it. Is the data more relational (think of links to other data), or a hierarchy? If the data you are storing needs to mix a lot with relationally stored data, it could become a pain.
Do you do complicated searches on these fields? There is powerful functionality for querying jsonb fields, but there will likely be complications and learning involved. I like the power of querying jsonb and manipulating it in my queries, but it's never as easy or natural to me as regular SQL.
Will your ORM and any other tools like query builders play well with it? It's an especially important question if you are using older technology. If you use an ORM, it may become annoying to have to work with plain SQL just to do jsonb queries.
Lazy loading is another consideration. If you have a lazy load strategy, it may or may not translate well into jsonb fields. If your lazy loading strategy is complex, it may require some work to get it to work well with jsonb fields.
A good strategy for serialization and deserialization will be necessary. If you don't have a good implementation of that, it will get clumsy.
By the way...I am a big advocate for using jsonb. In my opinion, it has been a good game changer. So don't think that I am discouraging it. That said, if you just try to make everything jsonb, you will probably soon regret it. You just need to sort of evaluate what works best for each case. In general, I think it does work well for config, usually, though I'd have to know about the particular case.I hope this helps a bit!
Upvotes: 2