django-grpc icon indicating copy to clipboard operation
django-grpc copied to clipboard

how serialize django model to gRPC message?

Open diegoduncan21 opened this issue 4 years ago • 9 comments

"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.

diegoduncan21 avatar Mar 05 '20 14:03 diegoduncan21

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

markdoerr avatar Apr 28 '20 10:04 markdoerr

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

gluk-w avatar May 11 '20 19:05 gluk-w

Thanks a lot, Stan, this looks reasonable. I will try it (hopefully) tonight. Cheers, mark

markdoerr avatar May 12 '20 06:05 markdoerr

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 ...

markdoerr avatar May 13 '20 20:05 markdoerr

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

gluk-w avatar May 14 '20 18:05 gluk-w

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 ?)

markdoerr avatar May 14 '20 20:05 markdoerr

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()

gluk-w avatar May 15 '20 00:05 gluk-w

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

sumitsharansatsangi avatar Jan 08 '22 02:01 sumitsharansatsangi

same confuse here

yswtrue avatar Aug 09 '23 13:08 yswtrue