question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add __visible__ to model serialization

See original GitHub issue

Right now, when serializing a model it will put all model fields + properties (what has been defined in append).

I have seen that we can start from a dict given as argument of the serialize() method, but I have not really see a way to define how the model should be serialized without using Masonite API.

Here I just want - on the fly - to serialize e.g. the User model with three attributes: id, name, email. name is a property. I feel we would like to do something like:

User.find(1).serialize(["id", "name", "email"]) # {"id": 1, "name": "John Doe", "email": johndoe@example.com}

OR we could also add something in the model such as : __serialized_fields__ = ["id", "name", "email"]

If you confirm it’s not possible right now and this implementation is correct, I’ll open a PR. We should also update documentation to explain model serialization I have not seen it in orm docs.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
josephmancusocommented, Jan 3, 2021

yeah that’s fine. I deff see a valid scenario for both. We do the same exact thing for the fillable and guarded attributes

0reactions
josephmancusocommented, Jan 5, 2021

Sure it might be too simple for you so if its not taken by someone on Friday go for it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial 1: Serialization - Django REST framework
We can also serialize querysets instead of model instances. To do so we simply add a many=True flag to the serializer arguments.
Read more >
Serializer as Field is not visible in json - Stack Overflow
How can I get nested object of the related model in json . I also tried an example from the docs but the...
Read more >
Eloquent: Serialization - The PHP Framework For Web Artisans
To hide relationships, add the relationship's method name to your Eloquent model's $hidden property. Alternatively, you may use the visible property to define ......
Read more >
Serializers - Django REST framework - Tom Christie
Serializers allow complex data such as querysets and model instances to be ... To do any other validation that requires access to multiple...
Read more >
Custom binary serialization | Microsoft Learn
During deserialization, SerializationInfo is passed to the class using the constructor provided for this purpose. Any visibility constraints ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found