Only queryset optimization in django
#10019
|
I like DRF and have been using it for my day to day work. Think query optimization adding Consider an example here: class User(models.Model):
name=models.CharField()
image = models.CharField() # s3 key
address= models.CharField()
email= models.CharField()
password= models.CharField()
# Bad db design here but did this to prove a point
settings_preferences = models.JsonField() # very big json (imagine like 50 KBs)And then there is a serializer. Used to display user's information in a comment with may be only name and image s3 key only. class UserBreifSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["name", "image"]
# New Meta field suggestion here
# can be done case by case or globally via `settings.py`
optimize_query = TrueImplementation wise we'll check if the iterable passed to the queryset is a list or a queryset. In case of a query set and the serializer fields and model fields match. Then we will add
The above idea will add more value when class BlogSerializer(serializers.ModelSerializer):
author = UserBreifSerializer()
class Meta:
model = Blog
fields = ["author", "title", "summary", "published_at"] |
Replies: 2 comments
|
This is already possible, you don't need to do anything. Just make sure your queryset (defined in view as class var or via get_queryset method) uses correct only() parameter (or correct select_related in the case of blog serialization). Serializers only access specified fields anyway. If you wanna make sure that a big field is never accessed, you can use only() or defer() with the combination of FETCH_RAISE (introduced in Django 6.1), which will make accessing that field an exception (as opposed to making an additional database query). https://docs.djangoproject.com/en/dev/topics/db/fetch-modes/ |
|
You might also want to look at django-mantle and its DRF extension, which does pretty much what you want. |
You might also want to look at django-mantle and its DRF extension, which does pretty much what you want.