Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how serialize django model to gRPC message? #7

Open
diegoduncan21 opened this issue Mar 5, 2020 · 9 comments
Open

how serialize django model to gRPC message? #7

diegoduncan21 opened this issue Mar 5, 2020 · 9 comments

Comments

@diegoduncan21
Copy link

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

@markdoerr
Copy link

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

@gluk-w
Copy link
Owner

gluk-w commented May 11, 2020

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

@markdoerr
Copy link

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

@markdoerr
Copy link

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

@gluk-w
Copy link
Owner

gluk-w commented May 14, 2020

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

@markdoerr
Copy link

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

@gluk-w
Copy link
Owner

gluk-w commented May 15, 2020

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

@sumitsharansatsangi
Copy link

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

@yswtrue
Copy link

yswtrue commented Aug 9, 2023

same confuse here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants