django-grpc
django-grpc copied to clipboard
how serialize django model to gRPC message?
"There is an easy way to serialize django model to gRPC message using django_grpc.serializers.serialize_model."
i was reading the code trying to understand but i can't, do you have an example code of this?
thanks.
Hi Stan, first of all: thanks for this great project: it is very valuable and will be more in the future, if gRPC gains more ground ! I have the same problem as @diegoduncan21 : if you explain a little, what you had in mind with the serializers, we might help you writing an example. It sounds like a great idea to find a generic solution for CRUD actions via gRPC. Thanks, mark
I apologize for the late response!
I wanted to have universal solution for converting model instance to protobuf. But I don't even remember how it worked because never used it :(
I found a better approach by defining .to_protobuf()
method for models that need to be serialized. The method should return instance of protobuf like this:
from django.db import models
from myproto import page_pb2 # this is generated python code out of proto definition
class Page(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
def to_protobuf(self):
return page_pb2.Page(title=self.title, body=self.body)
Then in the servicer I can reuse that every time Page message must be returned.
Regards, Stan
Thanks a lot, Stan, this looks reasonable. I will try it (hopefully) tonight. Cheers, mark
Hi Stan, I started to implement your suggestion, but ran into some issues: How would you serialise Foreign keys or, even worse, ManyToMany fields in the way you proposed ? How would you create / update fields with ManyToMany relations or Foreign keys via gRPC ? Looks not trivial too me ...
You have to do that manually and copy model properties to protobuf structure like this:
def to_protobuf(self):
categories = [page_pb2.Category(title=cat.title) for cat in self.categories.all()]
return page_pb2.Page(title=self.title, body=self.body, categories=categories)
This is especially true if your django models differ from protobuf
Thanks, Stan, interesting construct: in your example categories will be a list of protobuf messages of type Category containing only the category title. And this list you put into the Page protobuf message - is the category information not lost now in your example case, because you will send a list of all categories with the Page message ? (ideally only the category of the page instance that is to be serialised should be returned - or am I missing something ?)
It depends on your protobuf structure. There can be one category per page or multiple, they can be defined as repeated strings or separate structures.
Ideally, there would be Category.to_protobuf() so it could be called in Page.to_prptobuf()
Hi Stan ,
In the documentation , I found
There is an easy way to serialize django model to gRPC message using django_grpc.serializers.serialize_model.
Can you please add an example how to use it?
Thank you Sumit
same confuse here