Initial opening: I am utilising postgresql JSONFields.
I have the following attribute
(field) in my User
model:
class User(AbstractUser):
...
benefits = JSONField(default=dict())
...
I essentially currently serialize benefits for each User on the front end with DRF:
benefits = UserBenefit.objects.filter(user=self)
serializer = UserBenefitSerializer(benefits, many=True)
As the underlying returned benefits changes little and slowly, I thought about "caching" the JSON in the database every time there is a change to improve the performance of the UserBenefit.objects.filter(user=user)
QuerySet. Instead, becoming user.benefits
and hopefully lightening DB load over 100K+ users.
1st Q:
Should I do this?
2nd Q:
Is there an efficient way to write the corresponding serializer.data
<class 'rest_framework.utils.serializer_helpers.ReturnList'>
to the JSON field?
I am currently using:
data = serializers.serialize("json", UserBenefit.objects.filter(user=self))