jazzband / django-ddp

Django/PostgreSQL implementation of the Meteor server.
MIT License
167 stars 29 forks source link

Questions about serialization of related model fields #43

Open codyparker opened 8 years ago

codyparker commented 8 years ago

Thanks for your time on this project. I'm getting back into working with it and it's really great! I had some questions about relations and what data is serialized and sent down to Meteor. I mentioned this before, but apparently related models through foreign keys only serialize and pass down the ID of the object, not access to the other fields.

For example, I have a model that is related to a User. In Meteor I would like to display the name of that user when referencing the object. I tried to add it through annotating with an F expression on the Django queryset, but that doesn't get serialized either. I do see in the api.py serialize method where you check for F expressions, but mine never match the isinstance(val, ExpressionNode) check as val is seen as a unicode string (in the case of the username). I don't know if this is a bug in that method or a user error on my end constructing the expression. The value is there, but it never gets serialized.

My workaround was that I just added in a bit at the end of your serialize method that finds all methods marked as a @property and then serialize and add them to the payload that's returned. This works great and I like to use properties on my models anyway, so this works fine for me. ( I can put up a pull request for this if you think it's useful )

But I'm surely missing something on how to do this otherwise -- so what is the recommended way to work with additional fields on related objects in this way?