Hi Diptesh

Django's serialization framework is mostly used for saving model data for
later loading into the DB - normally for setting up environments or tests.
I don't think saving properties is useful in this case. If you're using
serialization for API responses, look at Django REST Framework's
serializers. These are much more customizable and can serialize properties,
or any other attributes.

Thanks,

Adam

On Tue, 8 Jun 2021 at 03:36, Diptesh Choudhuri <diptesh.choudh...@gmail.com>
wrote:

> Hello, I hope you are all doing well.
>
> Currently if one wants to have custom properties on their models without
> introducing new columns, they have to use properties:
>
> class Chapter(models.Model):
>     name = models.CharField()
>     text = models.TextField()
>
>     @property
>     def word_count(self):
>         return len(self.text.split())
>
> chapter_one = Chapter.objects.first()
>
> Now, the word count can be accessed using *chapter_one.wordcount*.
> However, when actually serializing this object, the wordcount isn't
> included:
> {
>   "name":"Technology",
>   "text":"Some long text"
>   // "wordcount": 3 <- this is not included
> }
>
> This problem can be fixed by updating the resultant serialization yourself.
>
> serialized.update({ "wordcount": chapter_one.wordcount })
>
> However, this quickly gets difficult once you have many such properties.
> There are workarounds for this (eg: using the* inspect *module) but they
> are difficult to understand and quickly result in unreadable code.
>
> I propose we add a new decorator which will allow these properties to be
> included in the default object serialization.
>
> *@column_property **# imported from django*
> def word_count(self):
>     return len(self.text.split())
>
> What do you think about this change? I think it'd be good since this won't
> introduce any backwards incompatible change. This change will especially
> benefit those who are creating some kind of API since they won't have to
> write complex code for such a simple use case.
>
> I can open a ticket for this on djangoproject, and start working on this
> soon.
>
> Thanks
> Diptesh Choudhuri
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/b6f33375-cf76-421c-b6d4-3642d0bbb9f1n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/b6f33375-cf76-421c-b6d4-3642d0bbb9f1n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAMyDDM3GurX_dT%2BC8NhHCkF_3t0zfJ4rGAuCU1%2B_W1X7AGgmrw%40mail.gmail.com.
  • Add... Diptesh Choudhuri
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)

Reply via email to