Search code examples
pythondjangodjango-modelsdjango-signals

Django: How to modify a text field before showing it in admin


I have a Django Model with a text field. I would like to modify the content of the text field before it's presented to the user in Django Admin.

I was expecting to see signal equivalent of post_load but it doesn't seem to exist.

To be more specific:

I have a text field that takes user input. In this text field there is a read more separator. Text before the separator is going to go into introtext field, everything after goes into fulltext field.

At the same time, I only want to show the user 1 text field when they're editing the article.

My plan was to on_load read the data from introtext and fulltext field and combine them into fulltext textarea. On pre_save, I would split the text using the read more separator and store intro in introtext and remainder in fulltext.

So, before the form is displayed, I need to populate the fulltext field with

introtext + '<!--readmore-->' + fulltext

and I need to be able to do this for existing items.


Solution

  • There is no post_load because there is no load function.

    Loading of the instance is done in init function, therefore the right answer is to use post_init signal.